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 '[email protected]'
      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

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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...