I didn't notice that when you create a second story you'd just omit the blocks that were run in the previous scenario<br><br>This is my second scenario<br><br> Scenario "Failed creating a new topic due to error" do
<br> <br> Given "a user named", "Jon"<br> Given "a forum named", "General"<br> And "user logged in successfully"<br><br> When "creating a new topic titled", "Nicks Mom" do |title|
<br> post_via_redirect "/forums/#{@forum.to_param}/topics", :topic => { :title => nil, :body => nil }<br> end<br><br> Then "user should see", "topics/new" do |template|<br>
response.should render_template(template)<br> end<br> <br> And "page should show", "Error" do |title|<br> response.should have_text(/#{title}/)<br> end <br> end<br><br>Is there a better way to test for errors or would this be the appropriate way?
<br><br><br><div><span class="gmail_quote">On 9/23/07, <b class="gmail_sendername">Andrew WC Brown</b> <<a href="mailto:omen.king@gmail.com">omen.king@gmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
That''s much more clear<br><br>I noticed you omitted<br> And "user logged in successfully" do |path|<br> post_via_redirect "/session", :login => "Jon", :password => "your_momma"
<br> end<br><br>My user is required to be login so without this story the test will fail.<br>Now should this be in my story because I'm not testing the if login works.<br>Anyway to share it with all my stories or should I move this line into my create_user function?
<div><span class="e" id="q_11532b7ad4c915d7_1"><br><br><div><span class="gmail_quote">On 9/23/07, <b class="gmail_sendername">Pat Maddox</b> <<a href="mailto:pergesu@gmail.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
pergesu@gmail.com
</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On 9/22/07, Andrew WC Brown <<a href="mailto:omen.king@gmail.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">omen.king@gmail.com</a>> wrote:<br>> I've written a story and I run into a snag.
<br>> I've written the create_forum method and told it to set the id to 1
<br>> but when it creates the forum the id is autoincremented.<br>> My forums table is empty but the id keeps incrementing the newest record on<br>> creation.<br>> When I run the story it comes out as something like 59 which fails my story
<br>> because I'm asking it to look at /forums/1<br>><br>> I thought of using my named routes instead of specifying the path as a<br>> string.<br>> That worked up until the point when I reached:<br>>
<br>> When "creating a new topic titled", "Nicks Mom" do |title|<br>> post topics(@forum, ??? ), :topic => { :title => title, :body => "She is<br>> teh hotZ!" }<br>> end
<br>><br>> The problem was since its a nested routed I couldn't complete the post<br>> request without specifying the topic_id.<br>> But since the topic hasn't been created yet their is no way (that I know of)
<br>> to pass the id since I don't know what it will be.<br>><br>> I would think the better practice is to state the paths as strings instead<br>> of using the nested routes.<br>><br>><br>><br>> require
File.join(File.dirname(__FILE__), "helper")<br>><br>> Story "User creates a new topic", %{<br>> As a user<br>> I want to create a new topic<br>> So that I can dicuss Nick's Mom
<br>> }, :type => RailsStory do<br>><br>> Scenario "Successfully create a new topic in the 'General' forum" do<br>><br>> Given "a user named", "Jon" do |login|
<br>> @user = create_user login<br>> end<br>> Given "a forum named", "General" do |name|<br>> @forum = create_forum name<br>> puts @<a href="http://forum.id" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
forum.id
</a><br>> end<br>><br>> And "user logged in successfully and was redirect to", "/" do |path|<br>> post "/session/create", :login => "Jon", :password => "your_momma"
<br>> response.should redirect_to("/")<br>> end<br>><br>> And "user is looking at", "/forums/1" do |path|<br>> get path<br>> response.should be_success
<br>> end<br>><br>> When "creating a new topic titled", "Nicks Mom" do |title|<br>> post "/forums/1/topics/1/create", :topic => { :id => 1, :title =><br>> title, :body => "She is teh hotZ!" }
<br>> end<br>><br>> Then "user should be redirected to", "/forums/1/topics/1" do |path|<br>> get path<br>> response.should be_success<br>> end<br>><br>> end
<br>><br>> Scenario "Failed creating a new topic due to blank fields" do; end<br>> Scenario "Sticky a new thread" do; end<br>> Scenario "Annoucment as a new thread" do; end
<br>
> end<br>><br>> def create_user(login)<br>> user = User.create!(<br>> :login => login,<br>> :email => " <a href="mailto:your@momma.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
your@momma.com</a>",<br>> :password => "your_momma",
<br>> :password_confirmation => "your_momma",<br>> :display_name => "Jon")<br>> user<br>> end<br>><br>> def create_forum(name)<br>> forum = Forum.create!(<br>> :id => 1,
<br>> :name => name,<br>> :description => "Everything from the latest gossip to the coolest<br>> youtube videos.")<br>> forum<br>> end<br>><br>><br>> How do I stop the incrementing?
<br>><br>> _______________________________________________<br>> rspec-users mailing list<br>> <a href="mailto:rspec-users@rubyforge.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
rspec-users@rubyforge.org</a><br>> <a href="http://rubyforge.org/mailman/listinfo/rspec-users" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
http://rubyforge.org/mailman/listinfo/rspec-users</a><br>><br><br>You don't stop the incrementing (at least afaik). That's a db thing.<br>You'd have to drop and recreate the db between runs. The good news is
<br>that the instance variables that you created are available everywhere<br>within the Scenario.<br><br>I wrote a story that is very similar to yours. I only rebuilt the<br>bits that were relevant to the test:<br><br>require
File.join(File.dirname(__FILE__), "helper")<br><br>Story "User creates a new topic", %{<br> As a user<br> I want to create a new topic<br> So that I can dicuss Nick's Mom<br> }, :type => RailsStory do
<br><br> Scenario "Successfully create a new topic in the 'General' forum" do<br><br> Given "a user named", "Jon" do |login|<br> @user = create_user login<br> end<br> Given "a forum named", "General" do |name|
<br> @forum = create_forum name<br> end<br><br> When "creating a new topic titled", "Nicks Mom" do |title|<br> post_via_redirect "/forums/#{@forum.to_param}/topics", :topic =>
<br>{ :title => title }<br> end<br><br> Then "user should see", "topics/show" do |template|<br> response.should render_template(template)<br> end<br><br> And "page should show", "Nicks Mom" do |title|
<br> response.should have_text(/Title: #{title}/)<br> end<br> end<br>end<br><br>def create_user(name)<br> user = User.create! :name => name<br> user<br>end<br><br>def create_forum(name)<br> forum = Forum.create
!
(<br> :name => name,<br> :description => "Everything from the latest gossip to the coolest<br>youtube videos.")<br> forum<br>end<br><br>You can see I did a couple things differently:<br><br>* Use the forum ID in the url
<br>* Use post_via_redirect<br>* Specify the template and some text that should be shown<br><br>The first item solves the problem of the autoincrementing ID, at least<br>for the forum_id part of the URL.<br><br>Your story is actually a bit broken, imo. You POST the new topic and
<br>then GET a url. But how does the user know what that URL is? More<br>likely you're redirecting them to the right URL. You should use<br>post_via_redirect because the user makes a post and then follows the<br>redirect.
<br><br>You'll have a controller spec that specifies what the redirect url<br>should be. Brief example<br><br>it "should redirect to the topic's show page" do<br> do_post<br> response.should redirect_to(topic_url(@mock_topic))
<br>end<br><br>The story allows you to specify the external behavior, which is that<br>the user POSTs to a url and then gets redirected to a page showing the<br>topic. The controller spec lets you specify the internal behavior,
<br>which includes the correct redirect url.<br><br>hth<br><br>Pat<br>_______________________________________________<br>rspec-users mailing list<br><a href="mailto:rspec-users@rubyforge.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
rspec-users@rubyforge.org</a>
<br><a href="http://rubyforge.org/mailman/listinfo/rspec-users" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://rubyforge.org/mailman/listinfo/rspec-users</a><br></blockquote></div>
</span></div></blockquote></div><br><br clear="all"><br>-- <br>Monsterbox Productions<br>putting small businesses on-line<br><br>1319 Victoria Avenue East<br>Thunder Bay, Ontario P7C 1C3<br>Canada<br><br>Andrew WC Brown<br>
web-developer and owner<br><a href="mailto:andrew@monsterboxpro.com">andrew@monsterboxpro.com</a><br>P: 807-626-9009<br>F: 807-624-2705