I write spec for controllers in rails application. I want to write DRY spec for verifying request parameters check, rspec can be helpful to use shared_example_for. For example.
shared_example_for "verifying request parameters" do
it "should return success for required parameter" do
get @action, @required_parameters
response.should be_success
end
it "should return error for lack of required parameters" do
get @action, @lack_of_parameters
response.should 400
end
end
describe BlogController do
fixtures :blogs
describe "show" do
before(:each) do
@action = "show"
@required_parameters = {:month=>blogs(:one).month, :day=>blogs(:one).day}
@lack_of_parameters = {:month=>blogs(:one).month}
end
it_should_behave_like "verifying request parameters"
end
describe "edit" do
before(:each) do
@action = "edit"
@required_parameters = {:month=>blogs(:one).month, :day=>blogs(:one).day}
@lack_of_parameters = {:month=>blogs(:one).month}
end
it_should_behave_like "verifying request parameters"
end
end
I want to write more DRY like below
describe BlogController do
fixtures :blogs
{
:show=>{:month=>blogs(:one).month, :day=>blogs(:one).day},
:edit=>{:month=>blogs(:one).month, :day=>blogs(:one).day}
}.each |act, prams| do
describe "blog/#{act}" do
before(:each) do
@action = act
@required_parameters = params
@lack_of_parameters = params.reject{|k,v| k==:month }
end
it_should_behave_like "verifying request parameters"
end
end
end
end
This code has problem that "blogs" is not undefined. It is caused by fixtures out of scope on "describe" block. And "lack_of_parameters" is incomplete for lacking pattern. How to solve it or write DRY spec for verifying parameters.