我在 Rails 中有三个页面都显示相同的标题,因此需要完全相同的集成测试。
不要重复自己并编写看起来几乎完全相同的单独测试,这里最好的方法是什么?我尝试将共享断言放入模块中,但未能成功将其加载到每个测试场景中。
内衣:
class IntegrationTest
describe "page one" do
before { visit page_one_path }
it "should have a home page link" do
page.find_link "Home"
end
end
describe "page two" do
before { visit page_two_path }
it "should have a home page link" do
page.find_link "Home"
end
end
describe "page three" do
before { visit page_three_path }
it "should have a home page link" do
page.find_link "Home"
end
end
end
尝试将其干燥失败...
模块:
/lib/tests/shared_test.rb
module SharedTest
def test_header
it "should have a home page link" do
page.find_link "Home"
end
end
end
测试:
class IntegrationTest
include SharedTest
describe "page one" do
before { visit page_one_path }
test_header
end
describe "page two" do
before { visit page_two_path }
test_header
end
describe "page three" do
before { visit page_three_path }
test_header
end
end
我还没有完全弄清楚如何编写模块,所以这不起作用也就不足为奇了。有人可以指出我正确的方向吗?