Ruby on rails 消除跨上下文的重复rspec测试
假设我有各种RSpecRuby on rails 消除跨上下文的重复rspec测试,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,假设我有各种RSpeccontext块来对具有类似数据场景的测试进行分组 feature "User Profile" do context "user is active" do before(:each) { (some setup) } # Various tests ... end context "user is pending" do before(:each) { (some setup) } # Various tes
context
块来对具有类似数据场景的测试进行分组
feature "User Profile" do
context "user is active" do
before(:each) { (some setup) }
# Various tests
...
end
context "user is pending" do
before(:each) { (some setup) }
# Various tests
...
end
context "user is deactivated" do
before(:each) { (some setup) }
# Various tests
...
end
end
现在,我添加了一个新功能,我想添加一个简单的场景来验证当我单击用户页面上的某个链接时的行为
it "clicking help redirects to the user's help page" do
click_on foo_button
expect(response).to have('bar')
end
理想情况下,我希望为所有3种上下文添加此测试,因为我希望确保它在不同的数据场景下正确执行。但是测试本身并没有随着上下文的变化而变化,所以把它全部输入3次似乎是重复的
干涸此测试集的替代方案有哪些?我是否可以将新测试粘贴到某个模块中,或者RSpec是否具有一些内置功能,让我定义它一次并从每个上下文块调用它
谢谢 您可以使用共享的\u示例
。。。在spec/support/shared_examples.rb中定义它们
shared_examples "redirect_help" do
it "clicking help redirects to the user's help page" do
click_on foo_button
expect(response).to have('bar')
end
end
然后在每个上下文中输入
it_behaves_like "redirect_help"
您甚至可以将一个块传递给,它的行为类似于,然后使用操作
方法执行该块,该块对于每个上下文都是唯一的
您的共享\u示例
可能看起来像
shared_examples "need_sign_in" do
it "redirects to the log in" do
session[:current_user_id] = nil
action
response.should render_template 'sessions/new'
end
end
在你的上下文中,你可以用block来称呼它
describe "GET index" do
it_behaves_like "need_sign_in" do
let(:action) {get :index}
end
...
您可以使用共享的\u示例
。。。在spec/support/shared_examples.rb中定义它们
shared_examples "redirect_help" do
it "clicking help redirects to the user's help page" do
click_on foo_button
expect(response).to have('bar')
end
end
然后在每个上下文中输入
it_behaves_like "redirect_help"
您甚至可以将一个块传递给,它的行为类似于,然后使用操作
方法执行该块,该块对于每个上下文都是唯一的
您的共享\u示例
可能看起来像
shared_examples "need_sign_in" do
it "redirects to the log in" do
session[:current_user_id] = nil
action
response.should render_template 'sessions/new'
end
end
在你的上下文中,你可以用block来称呼它
describe "GET index" do
it_behaves_like "need_sign_in" do
let(:action) {get :index}
end
...