On Dec 23, 2007 7:38 AM, Ivo Dancet <ivo.dancet at gmail.com>
wrote:> Hi all
>
> I just recently started to use rspec and I''m having a problem
using
> multple scenarios in one story. These are the two scenarios, trying to
> test my implemenation of the new http authentication in rails 2:
>
> Scenario "user has to authenticate" do
> Given "an anonymous user" do
> end
> When "visiting", "working_page" do |page|
> get page
> end
> Then "it should return 401 Unauthorized on each request" do
> response.headers["Status"].should == "401
Unauthorized"
> end
> end
>
> Scenario "user authenticates so the page should return 200" do
> Given "a user"; end
> When "visiting", "working_page" do |page|
> get page, nil, :authorization =>
>
ActionController::HttpAuthentication::Basic.encode_credentials("name",
> "pass")
> end
> Then "I''m logged in" do
> response.should be_success
> end
> end
>
> What happens is that the second scenario''s response is also
returning
> 401 and if I switch the order of the two scenarios, the second
> scenario (now the one that should return 401) returns 200. Each
> scenario works if I remove the other one.
Within a single story, each step can only get defined once. The second
definition is bypassed. There is a discussion about this in a ticket
about this at the lighthouse
(http://rspec.lighthouseapp.com/projects/5645/tickets/167) and we''ll
probably add a warning when you try to do this, but know that it is
not supported and will not be supported - so you''ll want to
restructure these scenarios.
On another note, an important factor of stories/scenarios is that the
doc-strings tell the story, so to speak. What you''ve got there would
not really do that very clearly. I''d probably try to do something more
like:
Scenario "user has to authenticate" do
Given "an anonymous user" do
@authorization => nil
end
When "visiting", "working_page" do |page|
get page, :authorization => @authorization
end
Then "it should return 401 Unauthorized on each request" do
response.headers["Status"].should == "401
Unauthorized"
end
end
Scenario "user authenticates so the page should return 200" do
Given "an authorized user" do
@authorization
ActionController::HttpAuthentication::Basic.encode_credentials("name",
"pass")
end
When "visiting", "working_page"
Then "I''m logged in" do
response.should be_success
end
end
Now the notion of the anonymous or authorized user is tied to the
correct step in each scenario.
I haven''t run that myself, so it might not work as/is, but it is the
direction I''d try to go.
Cheers,
David
>
> thanks for any help!
> Ivo Dancet
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>