gpt4 book ai didi

ruby-on-rails - 工厂女郎 + RSpec : undefined method 'create' when create(:user)

转载 作者:数据小太阳 更新时间:2023-10-29 06:36:51 24 4
gpt4 key购买 nike

不能调用“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

最佳答案

您需要将 create 行移动到它正在使用的规范中:

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

现在,该行没有上下文(不在规范中,也不在 before block 中)。这可能就是您收到错误的原因。您可能所有设置都是正确的,但只有一行的位置错误。

关于ruby-on-rails - 工厂女郎 + RSpec : undefined method 'create' when create(:user),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22751389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com