【发布时间】:2015-03-06 22:23:29
【问题描述】:
我的控制器规范中有很多条件和相同的期望:
if condition 1 - expect(reponse).to redirect_to same_url
if condition 2 - expect(reponse).to redirect_to same_url
if condition 3 - expect(reponse).to redirect_to same_url
RSpec 的 DRY 规则建议使用“上下文”而不是“如果条件”。 好的,这是我的控制器规格:
RSpec.describe MyController, type: :controller do
describe ".method" do
context "when wrong hash" do
it "redirect to error_url" do
get :method, key: '123', hash: 'wrong_hash'
expect(subject).to redirect_to error_url
end
end
context "when status is blocked" do
it "redirect to error_url" do
get :method, key: '123', hash: valid_hash, status: 'blocked'
expect(subject).to redirect_to error_url
end
end
context "when status is expired" do
it "redirect to error_url" do
get :method, key: '123', hash: valid_hash, status: 'expired'
expect(subject).to redirect_to error_url
end
end
end
end
正如我在上面所写的,我在多种情况下都有相同的重复“它应该”和相同的期望。如何“干燥”它?
【问题讨论】:
标签: ruby-on-rails ruby rspec dry