Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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

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

大佬总结

以上是大佬教程为你收集整理的ruby-on-rails – Factory_Girl RSpec:创建(:user)时的未定义方法’create’全部内容,希望文章能够帮你解决ruby-on-rails – Factory_Girl RSpec:创建(:user)时的未定义方法’create’所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: