gpt4 book ai didi

ruby-on-rails - Rspec工厂少女faker不接受专栏

转载 作者:太空宇宙 更新时间:2023-11-03 16:46:40 27 4
gpt4 key购买 nike

我将 RSpec 与 FactoryGirl 和 Faker 一起使用。我有以下错误:

myrailsexp/spec/factories/contacts.rb:5:in `block (2 levels) in <top (required)>': undefined method `first_name' for #<FactoryGirl::Declaration::Implicit:0x007fa205b233c0> (NoMethodError)

这是模型 app/models/contact.rb:

class Contact < ActiveRecord::Base

attr_accessible :first_name, :last_name

validates :first_name, presence: true
validates :last_name, presence: true

end

规范/模型/contact_spec.rb

require 'rails_helper'

RSpec.describe Contact, :type => :model do
it "has a valid factory" do
Factory.create(:contact).should be_valid
end
it "is invalid without a first_name"
it "is invalid without a last_name"
it "returns a contact's full_name as a string"
end

规范/工厂/contacts.rb

require 'faker'

FactoryGirl.define do
factory :contact do
f.first_name { Faker::Name.first_name }
f.last_name { Faker::Name.last_name }
end

end

谢谢

最佳答案

这个

require 'faker'

FactoryGirl.define do
factory :contact do
f.first_name { Faker::Name.first_name }
f.last_name { Faker::Name.last_name }
end

end

应该是

require 'faker'

FactoryGirl.define do
factory :contact do |f|
f.first_name { Faker::Name.first_name }
f.last_name { Faker::Name.last_name }
end

end

还有这一行

Factory.create(:contact).should be_valid

应该是

FactoryGirl.create(:contact).should be_valid 

关于ruby-on-rails - Rspec工厂少女faker不接受专栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31660152/

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