ruby-on-rails – Factory_Girl RSpec:创建(:user)时的未定义方法’create’

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Factory_Girl RSpec:创建(:user)时的未定义方法’create’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
无法调用“dummy = create(:user)”来创建用户.已经走了几个小时.
/home/parreirat/backend-clone/Passworks/spec/models/user_spec.rb:15:in `block (2 levels) in <top (required)>': undefined method `create' for #<Class:0xbcdc1d8> (NoMethodError)"

这是工厂,users.rb:

FactoryGirl.define do

    factory :user do
      email 'heheheheheheh@gmail.com'
      password 'chucknorris'
      name 'luis mendes'
    end

end

这就是我在user_spec.rb中调用FactoryGirl的方法

require 'spec_helper'

describe 'User system:' do

 context 'registration/login:' do

    it 'should have no users registered initially.' do
      expect(User.count).to eq(0)
    end

    it 'should not be logged on initially.' do
      expect(@current_user).to eq(nil)
    end

    dummy = create(:user)

    it 'should have a single registered user.' do
      expect(User.count).to eq(1)
    end

  end

end

我按照说明将其添加到spec_helper.rb中:

RSpec.configure do |config|

   # Include FactoryGirl so we can use 'create' instead of 'FactoryGirl.create'
   config.include FactoryGirl::Syntax::Methods

end

解决方法

您需要在其中使用的规范中移动创建行:
it 'should have a single registered user.' do
  dummy = create(:user)
  expect(User.count).to eq(1)
end

现在,该行没有上下文(不在规范中,而不是在前一个块中).这可能是为什么你得到错误.你可能所有的设置正确,但只有一行在错误的地方.

原文链接:https://www.f2er.com/ruby/272869.html

猜你在找的Ruby相关文章