Rspec Test Failing?

466 views
Skip to first unread message

fuzzy

unread,
Jan 15, 2013, 6:09:35 PM1/15/13
to rubyonra...@googlegroups.com
Hi,
 
rails 3.2.11 and Capybara 2.0.2 and the latest rspec.
 
I have another test that has me searching for a solution ... just now, I do not have one and I have spent a number of days trying out different solutions.
 
If I may ... here is the test, spec/features:
 
           describe "submitting a PUT request to the Employees#update action" do
              before { put employee_path(employee) }
              specify { response.should redirect_to(signin_path) }
            end
 
It fails with this message:
 
             NoMethodErrror: undefined method 'put'
 
In this same group, I have another two tests failing both with:
 
            undefined local variable or method 'cookies'
 
and it identifies the sign_in def as the culprit:
 
      
           def sign_in(employee)
               visit signin_path
                  fill_in "Email", with: employee.email
                  fill_in "Password", with: employee.password
                  click_button "Sign in"
                  # Sign in when not using Capybara.
                  cookies[:remember_token] = employee.remember_token
             end 
 
This is part of the spec/support/utilities file.
 
Does anyone have any ideas on how to solve this?
 
 
Thanks.
 
 
                 

fuzzy

unread,
Jan 16, 2013, 6:05:44 PM1/16/13
to Ruby on Rails: Talk
I just read that Capybara does not do integration testing ... this I
found right at the bottom of the Capybara documentation page. So since
all Capybara tests reside in the spec/features folder, I moved the

submitting a PUT request to the Employees#update

test into the spec/request folder and it passes.

Some progress .. now for the other two failing tests ... namely the:

undefined local variable or method 'cookies'

Some more research of maybe someone has an understanding of why this
test is failing and is willing to give me some guidance?

Thanks.

Mirri Kim

unread,
Jan 18, 2013, 11:17:59 AM1/18/13
to rubyonra...@googlegroups.com
Hi,

`specify` is actually just the same as `it`. It's just there because sometimes it reads better. See here: https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/example_group.rb#L82

- Mirri

On Fri, Jan 18, 2013 at 5:22 PM, <and...@benjamin.dk> wrote:
Hey fuzzy,

I think the first thing that is maybe wrong with your spec is that you are trying to do a request inside of a describe.

it should be something like:

describe "#update" do
before { put employee_path(employee) }
it "returns something or does something" do
specify { response.should redirect_to(signin_path) }
end
end

also whats the whole spec file you are running and in which like you get and which error exactly: please copy all this details

all the best,

Andre
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/iNBNpjYdySMJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

fuzzy

unread,
Jan 18, 2013, 11:47:03 AM1/18/13
to Ruby on Rails: Talk
Hi Andre,

Thanks for your reply ... following is the
authentication_pages_spec.rb file which now resides in the spec/
features folder ... all of the tests pass except three.

require 'spec_helper'

describe "Authentication" do

subject { page }

describe "signin" do
before { visit signin_path }

it { should have_selector('h2', text: 'Sign in') }
it { should have_title('Sign in') }


describe "with invalid information" do
before { click_button "Sign in" }

it { should have_title('Sign in') }
it { should have_error_message('Invalid') }

describe "after visiting another page" do
before { click_link "Home" }
it { should_not have_selector('div.flash.error', text:
'Invalid') }
end
end

describe "non_admin employee with valid information" do
let(:employee) { FactoryGirl.create(:employee) }
before { valid_signin(employee) }

it { should have_title(employee.emp_full_name) }

it { should_not have_link('Employees', href:
employees_path) }
it { should_not have_link('New Employee', href:
newemployee_path) }
it { should_not have_link('New Contract', href:
new_contract_path) }

it { should have_link('Sign out', href: signout_path) }

it { should_not have_link('Sign in', href: signin_path) }

describe "followed by signout" do
before { click_link "Sign out" }
it { should have_link('Sign in') }
end
end

describe "admin employee with valid information" do
let(:admin) { FactoryGirl.create(:admin) }
before { valid_signin(admin) }

it { should have_title(admin.emp_full_name) }

it { should have_link('Employees', href:
employees_path) }
it { should have_link('Profile', href:
employee_path(admin)) }
it { should have_link('Settings', href:
edit_employee_path(admin)) }
it { should have_link('New Employee', href:
newemployee_path) }
it { should have_link('New Contract', href:
new_contract_path) }

it { should have_link('Sign out', href: signout_path) }

it { should_not have_link('Sign in', href: signin_path) }

describe "Employee" do
it "logs factory girl generated objects" do
admin = FactoryGirl.create( :admin )
logger.warn( admin.pretty_inspect )
end
end
end
end

describe "Authorization" do

describe "for non-signed-in employees" do
let(:employee) {FactoryGirl.create(:employee) }

describe "in the Employees controller" do

describe "visting the edit page" do
before { visit edit_employee_path(employee) }
it { should have_title('Sign in') }
end

describe "submitting to the update action" do
before { put employee_path(employee) } # the put gets
us to the update
specify { response.should redirect_to(signin_path) }
end
end

describe "as wrong employee" do
let(:employee) { FactoryGirl.create(:employee) }
let(:wrong_employee) { FactoryGirl.create(:employee,
email: "wr...@example.com") }
before { sign_in employee }

describe "visiting Employees#edit page" do
before { visit edit_employee_path(wrong_employee) }
it { should have_title('Edit') }
end

describe "submitting a PUT request to the
Employees#update action" do
before { put employee_path(wrong_employee) }
specify { response.should redirect_to(root_path) }
end
end

describe " when attempting to visit a protected page" do
before do
visit edit_employee_path(employee)
fill_in "Email", with: employee.email
fill_in "Password", with: employee.password
click_button "Sign in"
end

describe "after signing in" do

it "should render the desired protected page" do
page.should have_title('Edit')
end

describe "when signing in again" do
before do
visit signin_path
fill_in "Email", with: employee.email
fill_in "Password", with: employee.password
click_button "Sign in"
end

it "should render the default (profile/show) page" do
page.should have_title(employee.emp_full_name)
end
end
end
end
end
end

describe "visiting employee index" do
before { visit employees_path }
it { should have_title('Sign in') }
end

describe "Employee" do
it "logs factory girl generated objects" do
admin = FactoryGirl.create( :admin )
logger.warn( admin.pretty_inspect )
end
end
end

This is the error that I am getting for three of the failures:

......................FFF....

Failures:

1) Authentication Authorization for non-signed-in employees in
the Employees controller submitting to the update action
Failure/Error: before { put employee_path(employee) } # the put
gets us to the update
NoMethodError:
undefined method `put' for
#<RSpec::Core::ExampleGroup::Nested_1::Nested_2::Nested_1::Nested_1::Nested_2:0xb6bf3c48>
# ./spec/features/authentication_pages_spec.rb:84:in `block (6
levels) in <top (required)>'

2) Authentication Authorization for non-signed-in employees as
wrong employee visiting Employees#edit page
Failure/Error: before { sign_in employee }
NameError:
undefined local variable or method `cookies' for
#<RSpec::Core::ExampleGroup::Nested_1::Nested_2::Nested_1::Nested_2::Nested_1:0xb6819438>
# ./spec/support/utilities.rb:36:in `sign_in'
# ./spec/features/authentication_pages_spec.rb:92:in `block (5
levels) in <top (required)>'

3) Authentication Authorization for non-signed-in employees as
wrong employee submitting a PUT request to the Employees#update
action
Failure/Error: before { sign_in employee }
NameError:
undefined local variable or method `cookies' for
#<RSpec::Core::ExampleGroup::Nested_1::Nested_2::Nested_1::Nested_2::Nested_2:0xb6916d2c>
# ./spec/support/utilities.rb:36:in `sign_in'
# ./spec/features/authentication_pages_spec.rb:92:in `block (5
levels) in <top (required)>'

Finished in 8.39 seconds
29 examples, 3 failures

Failed examples:

rspec ./spec/features/authentication_pages_spec.rb:85 #
Authentication Authorization for non-signed-in employees in the
Employees controller submitting to the update
action
rspec ./spec/features/authentication_pages_spec.rb:96 #
Authentication Authorization for non-signed-in employees as wrong
employee visiting Employees#edit page
rspec ./spec/features/authentication_pages_spec.rb:101 #
Authentication Authorization for non-signed-in employees as wrong
employee submitting a PUT request to the
Employees#update action

and here is the utilities.rb file to be found in the spec/support
folder:

include ApplicationHelper

def valid_signin(employee)
fill_in "Email", with: employee.email
fill_in "Password", with: employee.password
click_button "Sign in"
end

RSpec::Matchers.define :have_error_message do |message|
match do |page|
page.should have_selector('div.flash.error', text: message)
end
end

Rspec::Matchers.define :be_accessible do |attribute|
match do |response|
response.class.accessible_attributes.include?(attribute)
end
description { "be accessible :#{attribute}" }
failure_message_for_should { ":#{attribute} should be
accessible" }
failure_message_for_should_not{ ":#{attribute} should not be
accessible" }
end

Rspec::Matchers::define :have_title do |text|
match do |page|
Capybara.string(page.body).has_selector?('title', text: text)
end
end

def sign_in(employee)
visit signin_path
fill_in "Email", with: employee.email
fill_in "Password", with: employee.password
click_button "Sign in"
# Sign in when not using Capybara.
cookies[:remember_token] = employee.remember_token
end

def valid_signin(admin)
fill_in "Email", with: admin.email
fill_in "Password", with: admin.password
click_button "Sign in"
end

I did try and move the:

describe "submitting to the update action" do

test to the spec/request folder and it passes in the request folder
but fails in the features folder and I think that has something to do
with the fact that Capybara does not do integration tests or to put it
another way ... you cannot get access to your controller using
Capybara ... that is just me speculating.

Thanks for having a look at this.






On Jan 18, 3:22 am, and...@benjamin.dk wrote:
> Hey fuzzy,
>
> I think the first thing that is maybe wrong with your spec is that you are
> trying to do a request inside of a describe.
>
> it should be something like:
>
> describe "#update" do
> before { put employee_path(employee) }
> it "returns something or does something" do
> specify { response.should redirect_to(signin_path) }
> end
> end
>
> also whats the whole spec file you are running and in which like you get
> and which error exactly: please copy all this details
>
> all the best,
>
> Andre
>
>
>
>
>
>
>
> On Thursday, 17 January 2013 00:05:44 UTC+1, fuzzy wrote:
>

fuzzy

unread,
Jan 18, 2013, 11:48:27 AM1/18/13
to Ruby on Rails: Talk
Thanks Mirri for you post ... I had a look at the link ... are they
saying that 'specify' has now been superceded by 'it'?


On Jan 18, 10:17 am, Mirri Kim <mirri....@gmail.com> wrote:
> Hi,
>
> `specify` is actually just the same as `it`. It's just there because
> sometimes it reads better. See here:https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/exampl...

Mirri Kim

unread,
Jan 18, 2013, 9:07:24 PM1/18/13
to rubyonra...@googlegroups.com
Nope, just an alias.

and...@benjamin.dk

unread,
Jan 22, 2013, 3:57:10 AM1/22/13
to rubyonra...@googlegroups.com

So basically I think you are near the solution:

request tests, allow you to test requests(like put and others):

when you doing integration testing at a feature level you dont make requests(you would have instantiate the controller which is not correct), you can instead maybe visit a page that makes that put request you want to test the redirect happening. does that makes sense to you?

fuzzy

unread,
Jan 22, 2013, 9:00:21 AM1/22/13
to Ruby on Rails: Talk
Hi Andre,

Thanks for your reply.

I did see a 'put_via_redirect'.

So I would have to rewrite the test and use this command and it would
be testing the redirect which is a way to test the put?

I will give that a try.

Thanks again.


On Jan 22, 2:57 am, and...@benjamin.dk wrote:
> So basically I think you are near the solution:
>
> request tests, allow you to test requests(like put and others):https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/reques...
>
> when you doing integration testing at a feature level you dont make
> requests(you would have instantiate the controller which is not correct),
> you can instead maybe visit a page that makes that put request you want to
> test the redirect happening. does that makes sense to you?
>
>
>
> On Saturday, 19 January 2013 03:07:24 UTC+1, Mirri Kim wrote:
>
> > Nope, just an alias.
>
> >> > > To post to this group, send email to rubyonra...@googlegroups.com<javascript:>
> >> .
> >> > > To unsubscribe from this group, send email to
> >> > > rubyonrails-ta...@googlegroups.com <javascript:>.
> >> > > To view this discussion on the web visit
> >> > >https://groups.google.com/d/msg/rubyonrails-talk/-/iNBNpjYdySMJ.
>
> >> > > For more options, visithttps://groups.google.com/groups/opt_out.
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "Ruby on Rails: Talk" group.
> >> To post to this group, send email to rubyonra...@googlegroups.com<javascript:>
> >> .
> >> To unsubscribe from this group, send email to
> >> rubyonrails-ta...@googlegroups.com <javascript:>.

and...@benjamin.dk

unread,
Jan 23, 2013, 3:48:19 AM1/23/13
to rubyonra...@googlegroups.com
That seems like it :)


all the best,

Andre

fuzzy

unread,
Jan 24, 2013, 8:56:35 AM1/24/13
to Ruby on Rails: Talk
Hey Andre,

Thanks for your link .. I have seen it before but I did not have the
same issue, but I am thinking that it might well be worth my effort to
explore using sessions instead of cookies.

Thanks again.

On Jan 23, 2:48 am, and...@benjamin.dk wrote:
> That seems like it :)
>
> also about your cookie problem, is there anything that might look like
> this:http://stackoverflow.com/questions/9960732/rspec-vs-cookies-the-test-...
Reply all
Reply to author
Forward
0 new messages