我决定尝试使用存根,因为我听说它们会使我的测试运行得更快:
他们已经离开了:
it "should create a new instance given a valid attribute" do
User.create!(@attr)
end
it "should require a name" do
no_name_user = User.new(@attr.merge(:name => ""))
no_name_user.should_not be_valid
end
it "should require an email address" do
no_email_user = User.new(@attr.merge(:email => ""))
no_email_user.should_not be_valid
end
对此:
it "should create a new instance given a valid attribute" do
user = FactoryGirl.build_stubbed(:user, @attr)
user.should be_valid
end
it "should require a name" do
no_name_user = FactoryGirl.build_stubbed(:user, @attr.merge(:name => ""))
no_name_user.should_not be_valid
end
it "should require an email address" do
no_email_user = FactoryGirl.build_stubbed(:user, @attr.merge(:email => ""))
no_email_user.should_not be_valid
end
但是要多花 10 秒钟才能运行。我的整个套房总共需要 5 分钟。我期待这样做会提高速度。