[rspec-users] Story Runner, autoincrementing

Jarkko Laine jarkster at gmail.com
Sun Sep 23 01:57:10 EDT 2007


Sorry for top posting, this is all I get from the mobile gmail.

You don't specify the id of the post when you create one. This is the
same for all restful create actions. In case of nested routes, you
give the parent resource as a param, with normal routes nothing:

post forum_posts_path(@forum)

Or

post "/forums/1/posts/"

That said, can't you also save the id when you create the object in a
given block?

//jarkko



On 9/23/07, Andrew WC Brown <omen.king at gmail.com> wrote:
> I've written a story and I run into a snag.
> I've written the create_forum method and told it to set the id to 1
> but when it creates the forum the id is autoincremented.
> My forums table is empty but the id keeps incrementing the newest record on
> creation.
> When I run the story it comes out as something like 59 which fails my story
> because I'm asking it to look at /forums/1
>
> I thought of using my named routes instead of specifying the path as a
> string.
> That worked up until the point when I reached:
>
> When "creating a new topic titled", "Nicks Mom" do |title|
>   post topics(@forum, ??? ), :topic => { :title => title, :body => "She is
> teh hotZ!" }
> end
>
> The problem was since its a nested routed I couldn't complete the post
> request without specifying the topic_id.
> But since the topic hasn't been created yet their is no way (that I know of)
> to pass the id since I don't know what it will be.
>
> I would think the better practice is to state the paths as strings instead
> of using the nested routes.
>
>
>
> require File.join(File.dirname(__FILE__), "helper")
>
> Story "User creates a new topic", %{
>     As a user
>     I want to create a new topic
>     So that I can dicuss Nick's Mom
>   }, :type => RailsStory do
>
>   Scenario "Successfully create a new topic in the 'General' forum" do
>
>     Given "a user named", "Jon" do |login|
>       @user = create_user login
>     end
>     Given "a forum named", "General" do |name|
>       @forum = create_forum name
>       puts @forum.id
>     end
>
>     And "user logged in successfully and was redirect to", "/" do |path|
>       post "/session/create", :login => "Jon", :password => "your_momma"
>       response.should redirect_to("/")
>     end
>
>     And "user is looking at", "/forums/1" do |path|
>       get path
>       response.should be_success
>     end
>
>     When "creating a new topic titled", "Nicks Mom" do |title|
>       post "/forums/1/topics/1/create", :topic => { :id => 1, :title =>
> title, :body => "She is teh hotZ!" }
>     end
>
>     Then "user should be redirected to", "/forums/1/topics/1" do |path|
>       get path
>       response.should be_success
>     end
>
>   end
>
>   Scenario "Failed creating a new topic due to blank fields" do; end
>   Scenario "Sticky a new thread" do; end
>   Scenario "Annoucment as a new thread" do; end
> end
>
> def create_user(login)
>   user = User.create!(
>     :login => login,
>     :email => "your at momma.com",
>     :password => "your_momma",
>     :password_confirmation => "your_momma",
>     :display_name => "Jon")
>   user
> end
>
> def create_forum(name)
>   forum = Forum.create!(
>     :id => 1,
>     :name => name,
>     :description => "Everything from the latest gossip to the coolest
> youtube videos.")
>   forum
> end
>
>
> How do I stop the incrementing?
>


-- 
--
Jarkko Laine
http://jlaine.net
http://odesign.fi


More information about the rspec-users mailing list