使用 RSpec 创建一些控制器测试,我发现自己为每个可能的用户角色重复了几个测试用例。
例如
describe "GET 'index'" do
context "for admin user" do
login_user("admin")
it "has the right title" do
response.should have_selector("title", :content => "the title")
end
end
context "for regular user" do
login_user("user")
it "has the right title" do
response.should have_selector("title", :content => "the title")
end
end
end
这是一个简单的例子,只是为了说明我的观点,但是我有很多重复的测试......当然也有一些测试对于每个上下文都是唯一的,但这并不重要。
有没有办法只编写一次测试,然后在不同的上下文中运行它们?