【发布时间】:2015-07-27 18:04:18
【问题描述】:
我将 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
spec/models/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
spec/factories/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
谢谢
【问题讨论】:
标签: ruby-on-rails ruby rspec factory-bot rspec-rails