我正在学习 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