【问题标题】:undefined method create for RSpec rails 5为 RSpec rails 5 创建未定义的方法
【发布时间】:2018-10-02 07:28:39
【问题描述】:

This 可能是重复的,但它对我不起作用。 请让我知道我错过了什么。

配置详情:

#Gemfile
gem 'factory_bot_rails' # 4.8.2

#spec/rails_helper.rb
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
  #other config
end

spec/factories/otps.rb

FactoryBot.define do
  factory :otp do
    phone { Faker::Number.number(10) }
    expiry { Time.now + Otp::Constants::EXPIRY_DURATION }
    password { Faker::Number.number(4) }
  end
end

spec/requests/authentication_controller_spec.rb

require 'spec_helper'
RSpec.describe "Authentication", :type => :request do
  describe "when invalid OTP is passed" do
    it "should return bad request" do
      otp = create(:otp) # ===> Throws error
    end
  end
end

【问题讨论】:

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


    【解决方案1】:

    更改您的规范以包含正确的助手:

    require 'rails_helper'
    
    RSpec.describe "Authentication", :type => :request do
      describe "when invalid OTP is passed" do
        it "should return bad request" do
          otp = create(:otp)
        end
      end
    end
    

    如果您使用 .rspec 文件,您也可以跳过该行,其中包含以下内容:

     --color
     --format progress
     --order random
     --require rails_helper
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-09
      • 1970-01-01
      • 2016-02-23
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多