ruby-on-rails - RSpec:FactoryBot 看到重复的定义

标签 ruby-on-rails rspec factory-bot rspec-rails

我对使用 FactoryBot 还很陌生,所以我可能会遗漏一些东西。我收到此错误消息: enter image description here

是否是由于spec_helper.rb文件中设置不当造成的?

至于定义 user.rb 工厂,我尝试在 user.rb 文件中包含“associations:contracts”。我仍然不确定我是否应该这样做,或者当前的格式是否适合 Rspec 获取与contracts.rb 的关联?

感谢任何帮助!谢谢!

spec_helper.rb

require 'factory_bot_rails'
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
  FactoryBot.definition_file_paths = [File.expand_path('../factories', __FILE__)]
  FactoryBot.find_definitions
  # rspec-expectations config goes here. You can use an alternate
  # assertion/expectation library such as wrong or the stdlib/minitest
  # assertions if you prefer.
  config.expect_with :rspec do |expectations|

规范/工厂/用户.rb

FactoryBot.define do
  factory :user do
    full_name "Test tester"
    email "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e99d8c9a9da99d8c9a9d8c9bc78a8684" rel="noreferrer noopener nofollow">[email protected]</a>"
    password "123456"
  end
end

规范/工厂/契约(Contract).rb

FactoryBot.define do
  factory :contract do
    vendor "O2"
    starts_on "2019-03-08"
    ends_on "2019-03-10"
    price 30
  end
end

规范/请求/contracts_api_spec.rb

require 'rails_helper'

RSpec.describe "ContractsApi", type: :request do

  describe "POST #create" do
    before(:each) do
      @user = FactoryBot.create(:user)
      @current_user = AuthenticateUserCommand.call(@user.email, @user.password)
      @contract = @current_user.contracts.create(vendor: "Lebara", starts_on: "2018-12-12", ends_on: "2018-12-14", price: "15")
    end


    it 'creates a new contract' do
      expect { post api_v1_contracts_path, params: @contract }.to change(Contract, :count).by(1)
    end
  end
end

最佳答案

我相信您不需要在 spec_helper.rb 中配置 FactoryBot,并且您在那里所做的事情可能会导致 FactoryBot 加载工厂两次。

尝试将 spec_helper.rb 的内容更改为:

RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods

  # rspec-expectations config goes here. You can use an alternate
  # assertion/expectation library such as wrong or the stdlib/minitest
  # assertions if you prefer.
  config.expect_with :rspec do |expectations| 

此外,考虑到您在测试中包含 FactoryBot::Syntax::Methods,您可以简单地使用 @user = create(:user) 而不是 @user = FactoryBot.create(:user)

关于ruby-on-rails - RSpec:FactoryBot 看到重复的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49589355/

相关文章:

ruby-on-rails - 升级到 RSpec 3 并遵循弃用通知会导致 "undefined method ' 允许'"

ruby-on-rails - Rails respond_with & Rspec Controller : Testing unsuccessful update

ruby-on-rails - 问题是由于 "accepts_nested_attributes_for"和 "has_many through"关系而发生的,它给出了 not id found error with factory_girl 和 rspec

ruby-on-rails - FactoryGirl 中的 find_or_initialize_by

ruby-on-rails - 错误: Relation does not exist when running Rails in production using postgresql

ruby-on-rails - 为企业客户混淆 MRI Ruby/JRuby 上的 Rails 应用程序

ruby-on-rails - rails 4 simpecov 丢失文件

ruby-on-rails - 如何判断一个字符串是不是数字?

ruby-on-rails - 如何干燥同一 Controller 中不同操作共享的 RSpec 测试

ruby-on-rails - 使用 RSpec 测试嵌套资源