[LRUG] Rspec help
Tom ten Thij
mail at tomtenthij.nl
Sun Aug 29 12:37:36 PDT 2010
> A friend has a question regarding how can they parameterise RSpec tests so they can test the same behaviour under slightly different conditions
> soo... if anyone can help:
>
> http://stackoverflow.com/questions/3595168
I responded:
The way I would do it in this case is to specify the request as a
lambda that performs it. That way I can refer to it in my shared specs
and set a different one for each type of request.
I like using rspec describe blocks when its sets an expectation, in
this case that a particular request method is used. The whole thing
will look something like this:
describe FooController do
shared_examples_for "any request" do
it "assigns foo" do
@request.call
assigns[:foo].should == "bar"
end
it "does not change the number of bars" do
@request.should_not change(Bar, :count)
end
end
context "using GET" do
before do
@request = lambda { get "index" }
end
it_should_behave_like "any request"
end
end
An even cleaner way is to use the 'let' construct, although it may be
a step too deep in rSpec magic for a novice:
describe FooController do
shared_examples_for "any request" do
it "assigns foo" do
request.call
assigns[:foo].should == "bar"
end
it "does not change the number of bars" do
request.should_not change(Bar, :count)
end
end
context "using GET" do
let(:request) { lambda { get "index" } }
it_should_behave_like "any request"
end
end
Tom.
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
Tom ten Thij
Unboxed Consulting, http://unboxedconsulting.com
T: +44 20 3137 2943 F: +44 20 7183 4251
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
More information about the Chat
mailing list