Chr*_*ini 2 rspec ruby-on-rails ruby-on-rails-3 fabrication-gem
我正在尝试使用rspec +制作进行简单的测试.不幸的是,没有太多体面的文章.
在spec/model/event_spec.rb中
require 'spec_helper'
describe Event do
subject { Fabricate(:event) }
describe "#full_name" do
its(:city) { should == "LA" }
end
end
Run Code Online (Sandbox Code Playgroud)
在spec/fabricators/event_fabricator.rb中
Fabricator(:event) do
user { Fabricate(:user) }
# The test works if I uncomment this line:
# user_id 1
city "LA"
description "Best event evar"
end
Run Code Online (Sandbox Code Playgroud)
在spec/fabricators/user_fabricator.rb中
Fabricator(:user) do
name 'Foobar'
email { Faker::Internet.email }
end
Run Code Online (Sandbox Code Playgroud)
我一直在:
1) Event#full_name city
Failure/Error: subject { Fabricate(:event) }
ActiveRecord::RecordInvalid:
Validation failed: User can't be blank
Run Code Online (Sandbox Code Playgroud)
PS如果有人知道任何在线文章/教程值得阅读关于rspec和制作的入门.请告诉我
Fabricator的一个特性是它懒惰地生成关联,这意味着User除非user在Event模型上调用访问器,否则不会生成关联.
看起来您的Event模型具有需要User存在的验证.如果是这种情况,您需要像这样声明您的制造商:
Fabricator(:event) do
# This forces the association to be created
user!
city "LA"
description "Best event evar"
end
Run Code Online (Sandbox Code Playgroud)
这样可以确保User模型与其一起创建Event,从而允许您的验证通过.
请参阅:http://fabricationgem.org/#!defining-fabricators