ruby-on-rails - FactoryGirl::AttributeDefinitionError:属性已定义:用户

标签 ruby-on-rails ruby rspec

我创建了工厂客户和契约(Contract)。我运行测试,但显示错误

FactoryGirl.define do
  factory :client, class: User do
    role 'client'
    first_name 'John'
    sequence(:last_name) {  |n| "client#{n}" }
    sequence(:email) { |n| "client#{n}@example.com" }
    # avatar { Rack::Test::UploadedFile.new(File.join(Rails.root, 'public', 'images', '128.jpg')) }
    password 'password'
    password_confirmation 'password'
  end
end

support/controller_macros.rb

module ControllerMacros
  def login_client
    before do
      @client = create(:client)
      #@request.env['devise.mapping'] = Devise.mappings[:client]
      sign_in @client
    end
  end
end

FactoryGirl.define do
  factory :contract do
    sequence(:title) { |n| "translation#{n}" }
    amount 150
    additional_information 'X' * 500
    due_date { 21.days.from_now }

    association :user, factory: :client
    association :user, factory: :contractor
  end
end

我运行测试 rspec 规范/controllers/contracts_controller_spec.rb

require 'rails_helper'

describe ContractsController do
  login_client
  let(:contract) { create(:contract) }

  describe 'POST #create' do

    context 'with valid attributes' do
      it 'redirects to payment page' do
        post :create, contract: attributes_for(:contract)
        expect(response).to redirect_to payment_new_path
      end
    end
  end
end

错误显示:

Failure/Error: post :create, contract: attributes_for(:contract)
  FactoryGirl::AttributeDefinitionError:
    Attribute already defined: user

工厂或测试有什么问题?

最佳答案

工厂 :contract 定义了两个名为 user 的属性,这是不允许的。

给它们唯一的(在工厂内)标签,例如:

FactoryGirl.define do
  factory :contract do
    sequence(:title) { |n| "translation#{n}" }
    amount 150
    additional_information 'X' * 500
    due_date { 21.days.from_now }

    association :client, factory: :client
    association :contractor, factory: :contractor
  end
end

因为它们看起来很合适,所以我选择了与工厂名称相对应的属性名称。这甚至可以通过省略工厂名称来缩短它:

FactoryGirl.define do
  factory :contract do
    sequence(:title) { |n| "translation#{n}" }
    amount 150
    additional_information 'X' * 500
    due_date { 21.days.from_now }

    client
    contractor
  end
end

(参见 http://www.rubydoc.info/gems/factory_girl/file/GETTING_STARTED.md,“关联”部分:

If the factory name is the same as the association name, the factory name can be left out.

)

关于ruby-on-rails - FactoryGirl::AttributeDefinitionError:属性已定义:用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38599077/

相关文章:

ruby-on-rails - Rails Migration 更改列以使用 Postgres 数组

javascript - jQuery 自动完成功能在 Rails 4 中不起作用

ruby - 防止错误导致RSpec测试失败

ruby-on-rails - rails 4 : Moving Active Record generated collections from view to model

ruby-on-rails - 在 Rails 3.1 中使用 rspec 测试嵌套资源 Controller

ruby - 未初始化的常量 Net::HTTPS (NameError)

ruby - Rails——使用末尾带有等号的方法?

javascript - 哪种脚本语言更适合嵌入多线程 C/C++ 应用程序

ruby-on-rails - 如何使用 xml 作为输入测试 Rails RESTful API 发布请求?

ruby - 我如何在不使用 ruby​​ 中的 sleep() 的情况下 rspec/test 一个 updated_at 字段?