Ruby 使用factory和rspec测试时取消查找方法firstname=
正在退出本教程。我正在添加另一个从rails应用程序教程开始的项目 在创建模型规格时 联系_spec.rbRuby 使用factory和rspec测试时取消查找方法firstname=,ruby,ruby-on-rails-4,factory-bot,rspec-rails,nomethoderror,Ruby,Ruby On Rails 4,Factory Bot,Rspec Rails,Nomethoderror,正在退出本教程。我正在添加另一个从rails应用程序教程开始的项目 在创建模型规格时 联系_spec.rb require 'spec_helper' describe Contact do it "has a valid factory" do FactoryGirl.create(:contact).should be_valid end it "is invalid without a firstname" it "is invalid without a lastname"
require 'spec_helper'
describe Contact do
it "has a valid factory" do
FactoryGirl.create(:contact).should be_valid
end
it "is invalid without a firstname"
it "is invalid without a lastname"
it "returns a contact's full name as a string"
end
工厂/联系人.rb
require 'faker'
FactoryGirl.define do
factory :contact do |f|
f.firstname { Faker::Name.first_name }
f.lastname { Faker::Name.last_name }
end
end
我运行rspec并收到此错误:
1) Contact has a valid factory
Failure/Error: FactoryGirl.create(:contact).should be_valid
NoMethodError:
undefined method `firstname=' for #<Contact name: nil, email: nil, content: nil>
# ./spec/models/contact_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 0.22028 seconds
4 examples, 1 failure, 3 pending
我的测试顺利通过,没有错误:
is invalid without a lastname
has a valid factory
is invalid without a firstname
returns a contact's full name as a string (PENDING: Not yet implemented)
Pending:
Contact returns a contact's full name as a string
# Not yet implemented
# ./spec/models/contact_spec.rb:15
Finished in 0.22146 seconds
4 examples, 0 failures, 1 pending
谢谢你sonnyhe2002帮我度过了难关!非常感谢 您确定您的数据库中有
firstname
列吗?确保db/schema.rb
在contacts
表中有一个firstname
列,并确保您已经运行了rake db:test:prepare
给出的错误是自描述性的:您的电子邮件和内容属性上有一些验证器,但您没有在联系人
工厂中设置这些属性以使您的模型有效
编辑:请将其添加到您的联系人工厂:
f.email 'email@example.com'
f.content 'a content'
您需要向工厂添加电子邮件属性
FactoryGirl.define do
factory :contact do |f|
f.firstname { Faker::Name.first_name }
f.lastname { Faker::Name.last_name }
f.email { Faker::Internet.email }
f.content 'This is content'
end
end
由于您使用的是has_no_表,我认为factory无法将其保存到数据库中
您应该使用“build”而不是“create”
FactoryGirl.build(:contact).should be_valid
在您的模型中:
validates_presence_of :name
validates_presence_of :email
validates_presence_of :content
validates_format_of :email, :with => /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i
validates_length_of :content, :maximum => 500
<>所以,在制作<代码> FasyMyBuy/Cuff>用户时,你需要考虑以上事实。您只需处理电子邮件和姓名的显示,还需要处理长度小于500个字符的内容的显示。FactoryGirl现在被FactoryBot取代。工厂女孩不受欢迎
FactoryBot版本5及以后版本仅支持创建工厂时的动态属性
因此,解决方案是:-
FactoryBot.define do
工厂:用户做什么
名称{“Ron”}
电子邮件{”ron@gmail.com" }
密码{“ronY123”}
密码确认{“ronY123”}
结束
end
(ActiveRecord::Schema.define(version:0)do end)这是rake db:test:prepareAdding电子邮件后db/Schema.rb中的全部内容。添加电子邮件attr有帮助,我现在尝试查找内容的attr。我是否可以从控制器中的secure_参数中删除:content?因此,第一步是添加f.content,下一个错误我仍然需要解决我认为您还需要使用build,而不是检查编辑的答案添加工厂的两行,并从create更改为build,结果不会发生任何变化,仍显示“无法创建表”对象消息。由此看来,我认为你的建议是有益的。这有助于传递剩余的错误吗?不推荐分配静态属性。
describe Contact do
it "has a valid factory" do
FactoryGirl.build(:contact).should be_valid
end
it "is invalid without a firstname" do
FactoryGirl.build(:contact, lastname: nil).should_not be_valid
end
it "is invalid without a lastname" do
FactoryGirl.build(:contact, lastname: nil).should_not be_valid
end
it "returns a contact's full name as a string"
end
is invalid without a lastname
has a valid factory
is invalid without a firstname
returns a contact's full name as a string (PENDING: Not yet implemented)
Pending:
Contact returns a contact's full name as a string
# Not yet implemented
# ./spec/models/contact_spec.rb:15
Finished in 0.22146 seconds
4 examples, 0 failures, 1 pending
f.email 'email@example.com'
f.content 'a content'
FactoryGirl.define do
factory :contact do |f|
f.firstname { Faker::Name.first_name }
f.lastname { Faker::Name.last_name }
f.email { Faker::Internet.email }
f.content 'This is content'
end
end
FactoryGirl.build(:contact).should be_valid
validates_presence_of :name
validates_presence_of :email
validates_presence_of :content
validates_format_of :email, :with => /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i
validates_length_of :content, :maximum => 500