上下文“答案是正确的”做
before(:each) do
@answer = stub_model(Answer, :correct => true).as_new_record
assigns[:answer] = @answer
render "answers/summarize"
end
it "should display flashcard context properly" do
response.should contain("Quiz")
end
it "should summarize results" do
response.should contain("is correct")
end
端
上下文“答案不正确”执行
before(:each) do
@answer = stub_model(Answer, :correct => false).as_new_record
assigns[:answer] = @answer
render "answers/summarize"
end
it "should display flashcard context properly" do
response.should contain("Quiz")
end
it "should summarize results" do
response.should contain("is incorrect")
end
端
如何在上述两种情况下避免重复以下块?
it "should display flashcard context properly" do
response.should contain("Quiz")
端
答案 0 :(得分:0)
您的规范描述了您希望从代码中获得的行为 - 这个重复次数是可以的。
如果失控,请使用不同的上下文来隔离不同的期望。例如,无论答案是什么,您都可以将这些重复期望纳入一个只测试页面的新上下文。
答案 1 :(得分:0)
如果您真的想要完成一些测试,可以这样做:
def answer_tests
it "should display flashcard context properly" do
response.should contain "Quiz"
end
it "should do be awesome" do
response.should be_awesome
end
end
context "answer is correct" do
answer_tests
it "should summarize results" do
response.should contain "is correct"
end
end
context "answer is incorrect" do
answer_tests
it "should summarize results" do
response.should contain "is incorrect"
end
end
正如您所看到的,当您想要在方法中包含多个测试时,这非常方便。