Forum: RSpec ActiveRecord::RecordNotFound error Cucumber/Webrat

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Andrei E. (Guest)
on 2008-11-26 13:05
(Received via mailing list)
Hello,

I am bit new to Cucumber and Webrat and I have one issue that I can't
seem
to fix on my own. I would appreciate your help.

I have a controller called places and a model called Place. I use the
acts_as_tree plugin in order for a place to have more places within it.
Ex.
Georgia would have Cobb County, Cobb County would have Smyra City.

This is how my controller looks for adding  a new place:

# loading the parent place
before_filter :load_parent_place, :only => [:new, :create]
def load_parent_place
    @parent_place = Place.find(params[:parent_id])
  end

 # GET /places/new
  def new
    @place = @parent_place.children.create

    respond_to do |format|
      format.html # new.html.erb
    end
  end

  # POST /places
  def create
    @place = @parent_place.children.create(params[:place])

    respond_to do |format|
      if @place.save
        flash[:notice] = 'Place was successfully created.'
        format.html { redirect_to(@place) }
      else
        format.html { render :action => "new" }
      end
    end
  end


I have created the cucumber feature also, but it errors out:

 Scenario: Register new place
    Given I am on the new place page with parent place "Georgia"
    When I fill in "Name" with "Atlanta"
    And I press "Create"
    Then I should see "Atlanta"

For the first line starting with "Given", I tried to put this step, but
it
errors out with Couldn't find Place with ID=1
(ActiveRecord::RecordNotFound)

Given /I am on the new place page with parent place "(.*)"/ do |name|
  visits "/places/new?parent_id=1"
  Place.create! :name => name, :parent_id => 1
end

What am I doing wrong or how should I proceed with writing these steps
for
this action?

Thank you,
Andrei E. (Guest)
on 2008-11-26 16:15
(Received via mailing list)
Hello,

I am bit new to Cucumber and Webrat and I have one issue that I can't
seem
to fix on my own. I would appreciate your help.

I have a controller called places and a model called Place. I use the
acts_as_tree plugin in order for a place to have more places within it.
Ex.
Georgia would have Cobb County, Cobb County would have Smyra City.

This is how my controller looks for adding  a new place:

# loading the parent place
before_filter :load_parent_place, :only => [:new, :create]
def load_parent_place
    @parent_place = Place.find(params[:parent_id])
  end

 # GET /places/new
  def new
    @place = @parent_place.children.create

    respond_to do |format|
      format.html # new.html.erb
    end
  end

  # POST /places
  def create
    @place = @parent_place.children.create(params[:place])

    respond_to do |format|
      if @place.save
        flash[:notice] = 'Place was successfully created.'
        format.html { redirect_to(@place) }
      else
        format.html { render :action => "new" }
      end
    end
  end


I have created the cucumber feature also, but it errors out:

 Scenario: Register new place
    Given I am on the new place page with parent place "Georgia"
    When I fill in "Name" with "Atlanta"
    And I press "Create"
    Then I should see "Atlanta"

For the first line starting with "Given", I tried to put this step, but
it
errors out with Couldn't find Place with ID=1
(ActiveRecord::RecordNotFound)

Given /I am on the new place page with parent place "(.*)"/ do |name|
  visits "/places/new?parent_id=1"
  Place.create! :name => name, :parent_id => 1
end

What am I doing wrong or how should I proceed with writing these steps
for
this action?

Thank you,
David C. (Guest)
on 2008-11-26 16:21
(Received via mailing list)
On Wed, Nov 26, 2008 at 5:04 AM, Andrei E. 
<removed_email_address@domain.invalid> wrote:
>
>
>       if @place.save
>
>   visits "/places/new?parent_id=1"
>   Place.create! :name => name, :parent_id => 1
> end
>
> What am I doing wrong or how should I proceed with writing these steps for
> this action?

Try this:

Given /I am on the new place page with parent place "(.*)"/ do |name|
  parent = Place.create! :name => name
  visits "/places/new?parent_id=#{parent.id}"
end
Andrei E. (Guest)
on 2008-11-26 19:18
(Received via mailing list)
Thank you, that worked.

I have a new issue. I tried finding a solution for this everywhere. Only
source is this article:
http://afreshcup.com/2008/10/09/authentication-in-...

I am using Restful Authentication and I would like to login in Cucumber.
This is how my feature looks like:

Scenario: Register new place
    Given I am logged in as a user
    And I am on the new place page with parent place "Georgia"
    When I fill in "Name" with "Atlanta"
    And I press "Create"
    And I should see "Atlanta"

This is the step:

Given /I am logged in as a user/ do
   @current_user = Factory.define :user do |u|
     u.name 'the user'
     u.email 'removed_email_address@domain.invalid'
     u.login  'the_login'
     u.password 'password'
     u.password_confirmation 'password'
   end
   visits "/login"
   fills_in("login", :with => "the_login")
   fills_in("password", :with => "password")
   clicks_button("Log in")
end

# places controller

before_filter :login_required, :only => [:create]

  # GET /places/new
  def new
    @place = @parent_place.children.create

    respond_to do |format|
      format.html # new.html.erb
    end
  end

  # POST /places
  def create
    @place = @parent_place.children.create(params[:place])

    respond_to do |format|
      if @place.save
        flash[:notice] = 'Place was successfully created.'
        format.html { redirect_to(@place) }
      else
        format.html { render :action => "new" }
      end
    end
  end

The "Given I am logged in as a user" step passes, along with all the
steps,
except the last one (And I should see "Atlanta"). The problem is that
the
login page is shown and not the show page with the new place created.

What do I have to set in Cucumber so that it acts as if the user is
logged
in? In know that in Rspec :login_required should return true. What's the
equivalent of that in Cucumber?

Thank you
This topic is locked and can not be replied to.