我正在学习 RSpec,但我注意到我的代码中有很多重复项。以下只是许多其他例子中的两个例子。有没有一种方法可以创建共享测试而不必遍历每个单独的属性?
describe "validation" do
describe "user_id" do
it "should not be blank or nil" do
@comment.user_id = nil
@comment.should_not be_valid
@comment.user_id = " "
@comment.should_not be_valid
end
it "should an integer" do
@comment.user_id = "a"
@comment.should_not be_valid
end
end
describe "post_id" do
it "should not be blank or nil" do
@comment.post_id = nil
@comment.should_not be_valid
@comment.post_id = " "
@comment.should_not be_valid
end
it "should an integer" do
@comment.post_id = "a"
@comment.should_not be_valid
end
end
end
最佳答案
你可以试试这个:https://github.com/thoughtbot/shoulda-matchers
它为您的模型提供了许多简单的匹配器
关于ruby-on-rails - 如何干掉 Rails 的 RSpec 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15773874/