Is this a conflict in RoR tutorial test? - ruby-on-rails

Im following Michael Hartl's Ruby on Rails tutorial.
In his Listing 9.42, he shows a test for links to delete users, on index page.Ž
Test is supposed to make sure that user with admin: true attribute sees delete links on user's index page (user's listing page).
Also, admin (first user) is not supposed to see a link to delete himself.
Test code goes as follows:
describe "delete links" do
it { should_not have_link('delete') }
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) }
before do
sign_in admin
visit users_path
end
it { should have_link('delete', href: user_path(User.first)) }
it "should be able to delete another user" do
expect do
click_link('delete', match: :first)
end.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(admin)) }
end
end
What puzzles me about this code is this:
it seems only logical to me that the first it clause in the describe block
which mentions path to User.first (which is admin here, cause admin is first in database)
conflicts with the third it clause in the describe block, which requires
that link to admin's delete doesn't exist.
Am I missing something here?
I didnt even run the thing yet, but it seems to me it has to fail.

Not that it is shown in your question but the first it block has no conflict as the first user was created above this code.
let(:user){FactoryGirl.create(:user)}.
The third it block refers to when the admin is signed it should not have a delete button for itself.
here is the full spec for #index:
subject { page }
describe "index" do
let(:user) { FactoryGirl.create(:user) } #First User Created Here
before do
sign_in user
visit users_path
end
it { should have_title('All users') }
it { should have_content('All users') }
describe "pagination" do
.
.
.
end
describe "delete links" do
it { should_not have_link('delete') }
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) } #Admin Created here
before do
sign_in admin
visit users_path
end
it { should have_link('delete', href: user_path(User.first)) }
it "should be able to delete another user" do
expect do
click_link('delete', match: :first)
end.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(admin)) }
end
end
end

certainly you should run it to figure out what the actual case. As per my understanding, this depends on how your database cleaner works, if it clean database on every run test which is common case then the above specs will pass.

Related

How to access Factory value in RSpec?

I'm pretty new to RSpec, and I've hit a stumbling block which is probably really simple!
The action is that when the "Approve Band" link is clicked, the value of #band.validated should not be nil any more, I've got it working in my rails app, but can't get the test to work..
What am I missing?
describe "edit band via admin" do
let(:user) { FactoryGirl.create(:user) }
let(:band) { FactoryGirl.create(:band) }
before do
admin_sign_in user
visit edit_admin_band_path(band)
end
describe "approve band" do
before { click_link "Approve Band" }
its(#band.validated) { should_not be_nil }
end
end
You need to reload band to reflect changes in the database.
describe "approve band" do
before do
click_link "Approve Band"
band.reload
end
subject { band }
its(:validated) { should_not be_nil }
end

ruby on rails tutorial errors chapter 9

I have been working on the ruby on rails tutorial. I am totally new to this stuff. I am in chapter 9 and totally stuck. I am hoping someone might help me decipher the error messages. Up until now I have just searched them and figured it out but I think it would be more valuable to just learn how to decipher the error message to fix the problem. If it is too long or cumbersome to explain than than I totally understand if no one wants to take it on. I was unable to find something online that helped me with this on my own.
Here are the errors that I am currently getting
1) AuthenticationPages signin with valid information authorization for non-signed-in users when attempting to visit a protected page after signing in should render the desired protected page
Failure/Error: click_button "Sign in"
Capybara::ElementNotFound:
Unable to find button "Sign in"
# ./spec/requests/authentication_pages_spec.rb:61:in `block (7 levels) in <top (required)>'
2) AuthenticationPages signin with valid information authorization for non-signed-in users in the Users controller visiting the edit page
Failure/Error: it { should have_title('Sign in') }
expected #has_title?("Sign in") to return true, got false
# ./spec/requests/authentication_pages_spec.rb:76:in `block (8 levels) in <top (required)>'
3) AuthenticationPages signin with valid information authorization for non-signed-in users in the Users controller as wrong user visiting Users#edit page
Failure/Error: before { sign_in user, no_capybara: true }
NoMethodError:
undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_2::Nested_2::Nested_2::Nested_1::Nested_2::Nested_3::Nested_1:0xabbb9e4>
# ./spec/requests/authentication_pages_spec.rb:87:in `block (8 levels) in <top (required)>'
4) AuthenticationPages signin with valid information authorization for non-signed-in users in the Users controller as wrong user submitting a PATCH request to the User#update action
Failure/Error: before { sign_in user, no_capybara: true }
NoMethodError:
undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_2::Nested_2::Nested_2::Nested_1::Nested_2::Nested_3::Nested_2:0xa82d354>
# ./spec/requests/authentication_pages_spec.rb:87:in `block (8 levels) in <top (required)>'
Finished in 6.6 seconds
65 examples, 4 failures
Failed examples:
rspec ./spec/requests/authentication_pages_spec.rb:66 #
AuthenticationPages signin with valid information authorization for
non-signed-in users when attempting to visit a protected page after
signing in should render the desired protected page
rspec ./spec/requests/authentication_pages_spec.rb:76 #
AuthenticationPages signin with valid information authorization for
non-signed-in users in the Users controller visiting the edit page
rspec ./spec/requests/authentication_pages_spec.rb:91 #
AuthenticationPages signin with valid information authorization for
non-signed-in users in the Users controller as wrong user visiting
Users#edit page
rspec ./spec/requests/authentication_pages_spec.rb:96 #
AuthenticationPages signin with valid information authorization for
non-signed-in users in the Users controller as wrong user submitting a
PATCH request to the User#update action
authentication_pages_spec.rb
require 'spec_helper'
describe "AuthenticationPages" do
subject { page }
describe "signin page" do
before { visit signin_path }
it { should have_content('Sign in') }
it { should have_title('Sign in') }
end
describe "signin" do
before { visit signin_path }
describe "with invalid information" do
before { click_button "Sign in" }
it { should have_title('Sign in') }
it { should have_selector('div.alert.alert-error', text: 'Invalid') }
describe "after visiting another page" do
before { click_link "Home" }
it { should_not have_selector('div.alert.alert-error', text: 'Invalid') }
end
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before do
fill_in "Email", with: user.email.upcase
fill_in "Password", with: user.password
click_button "Sign in"
end
it { should have_title(user.name) }
it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Settings', href: edit_user_path(user)) }
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
describe "authorization" do
describe "for non-signed-in users" do
let(:user) { FactoryGirl.create(:user) }
describe "when attempting to visit a protected page" do
before do
visit edit_user_path(user)
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
describe "after signing in" do
it "should render the desired protected page" do
expect(page).to have_title('Edit user')
end
end
end
describe "in the Users controller" do
describe "visiting the edit page" do
before { visit edit_user_path(user) }
it { should have_title('Sign in') }
end
describe "submitting to the update action" do
before { patch user_path(user) }
specify { expect(response).to redirect_to(signin_path) }
end
describe "as wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong#example.com") }
before { sign_in user, no_capybara: true }
describe "visiting Users#edit page" do
before { visit edit_user_path(wrong_user) }
it { should_not have_title(full_title('Edit user')) }
end
describe "submitting a PATCH request to the User#update action" do
before { patch user_path(wrong_user) }
specify { expect(response).to redirect_to(root_url) }
end
end
end
end
end
end
end
end
user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
describe "signup" do
before { visit signup_path }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
describe "after submission" do
before { click_button submit }
it { should have_title('Sign up') }
it { should have_content('error') }
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email",with: "user#example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a new user" do
expect { click_button submit }.to change(User, :count).by(1)
end
describe "after saving the user" do
before { click_button submit }
let(:user) { User.find_by(email: 'user#example.com') }
it { should have_title(user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
describe "edit" do
let(:user) {FactoryGirl.create(:user) }
before do
sign_in user
visit edit_user_path(user)
end
describe "page" do
it { should have_content("Update your profile") }
it { should have_title("Edit user") }
it { should have_link('change', href: 'http://gravatar.com/emails') }
end
describe "with invalid information" do
before { click_button "Save changes" }
it { should have_content('error') }
end
describe "with valid information" do
let(:new_name) { "New Name" }
let(:new_email) { "new#example.com" }
before do
fill_in "Name", with: new_name
fill_in "Email", with: new_email
fill_in "Password", with: user.password
fill_in "Confirm Password", with: user.password
click_button "Save changes"
end
it { should have_title(new_name) }
it { should have_selector('div.alert.alert-success') }
it { should have_link('Sign out', href: signout_path) }
specify { expect(user.reload.name).to eq new_name }
specify { expect(user.reload.email).to eq new_email }
end
end
end
end
end
end
I am particularly curious what "in 'block(7 levels)in" means. Are the levels something on that page or is it referring to something else? What is 'The block' in reference to?
I also cannot seem to understand the capybara error that I am getting. It appeared in chapter 8, went away and now has come back. The button is there on the page when I bring it up so capybara is just not finding it I guess. Can anyone explain how that works?
What does the 'nested" refer to?
Anyone know a good website that breaks this down? I would be more than happy to do the work myself but I could not find one. I would really love to be able to decipher this myself instead of just googling it and hoping the answer is somewhere or having to rely on someone else to explain it all the time.
Thanks so much for your time and any help.
Now is the time on Stack Overflow when I oversimplify. (Google 'ruby block' to read a lot more on this.) A block in Ruby is a bunch of code that gets passed to a method like an argument. For example,
[1,2,3].each{|n| puts n * n }
each is the method (called on the array [1,2,3]), and everything in the brackets is the block. The way the method each works is, it takes every element in the enumerable it`s called on ([1,2,3]) and yields one element at a time to the code block:
|the first element is 1| puts 1 * 1
|next is 2| puts 2 * 2
|etc| puts 3 * 3
A block can also be written between do...end. The Ruby way is to use brackets if you can fit the block on one line, and do...end otherwise - which is just how you have it in your specs. Everywhere you have a do and a matching end in your specs is a block, nested one inside another. The methods are harder to notice, because RSpec makes it look like natural language, but every time you write describe or it at the start of a line is a method call. (So are let and before and subject and expect, for that matter, which get called with single line blocks in your specs.)
So the message 'block(7 levels)' means your error is nested in that many blocks:
describe "AuthenticationPages" do #starts the first block
...
describe "signup" do #starts the second
and so on.
Now, your error messages. The first and second are basically telling you the same thing - you visit edit_user_path(user) and you don't see a "Sign In" button or "Sign In" in the page title. Check the log/test.log file - what happens when you visit that page? Is it a redirect to the signin page? It ought to be, but it looks like it isn't.
The other two error messages say exactly the same thing - the spec doesn't know what sign_in means. You need to have a method by that name defined somewhere RSpec can find it - either in the spec itself, or the spec_helper file that you require at the top of the spec, or in some file which is itself required inside spec_helper.
Finally, I think Hartl is right - you Google as best you can with error messages and stack traces, ask when you can't find what you're looking for, and you'll get better figuring things out yourself with time.
Re: sign_in -- The sign_in function was added to spec/support/utilities.rb in section 9.1.1, Listing 9.6 (in Rails 4 version of book).
I got the same error because my function in utilities.rb was "signin" without the underscore. Once I added the underscore (and changed the other reference to the same function to match), the test went green.

How do I get this rspec test to pass?

I can't for the life of me figure out why these tests are failing.
When a user puts in their email/password and hits the Log in button, they are redirected to their profile page which puts their first name in the title and displays their first name on the page. It also shows a link to their profile and a sign out link. When I went through the steps in the browser everything was where it should be, but when rspec runs it continues to fail.
What I find really odd is that when I run a user_page_spec test that tests the same elements, those all pass.
I figure it has to do with either the click_button part or the "redirect_to user" in the controller, but any insight would be much appreciated.
Here are the tests-
Passing tests in user_pages_spec.rb-
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_selector('h1', text: user.firstName) }
it { should have_selector('title', text: user.firstName) }
end
Failing tests in authentication_pages_spec.rb -
require 'spec_helper'
describe "Authentication" do
describe "sign in" do
.
.
.
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before do
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Log in"
end
it { should have_selector('title', text:user.firstName) }
it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Sign out', href: signout_path) }
describe "followed by signout" do
before { click_link "Sign out" }
it { should have_link('Home') }
end
end
end
end
Yup. It's always the simplest of oversights that cause the biggest of headaches.
Here is what happened.
Rather than using the following-
describe "page" do
it "should have something" do
page.should have_selector('')
end
end
Rspec lets you define a subject -
subject { page }
Which allows you to simplify the first code block to the following-
subject { page }
describe "page" do
it { should have_selector('') }
end
This allows you to run multiple tests which reference the page without all the extra typing.
I left out the subject { page } at the very top, so none of my it {} blocks knew what to reference. As soon as that was added in, all tests passed with no problems.
Hope this helps someone else out in the future.

capybara/rspec: should GET/PUT tests be in different files?

I'm following the Ruby on Rails Tutorial, and now I need to write tests for the authorization code, e.g. making sure users can only edit their own profile.
There are two actions to test. One is to ensure a user can't access the page of editing other users' profile. This one is easy, a simple "feature" test in capybara.
But I certainly want to test the PUT action too, so that a user can't manually submit a PUT request, bypassing the edit page. From what I read, this should be done as an rspec "request" test.
Now my question is, do I have to maintain them in different dirs? (spec/features vs spec/requests)? It doesn't sound right since these two scenarios are closely related. How are such tests usually done in Rails?
For example,
describe "as wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong#example.com") }
before { sign_in user }
describe "visiting Users#edit page" do
before { visit edit_user_path(wrong_user) }
it { should_not have_selector('title', text: full_title('Edit user')) }
end
describe "submitting a PUT request to the Users#update action" do
before { put user_path(wrong_user) }
specify { response.should redirect_to(root_path) }
end
end
The second test doesn't work in capybara 2.x since "put" is not supported any longer. It has to be a request test. And now I have to write a second "sign_in" method, since the current one uses methods that are only available to feature tests. Smells like a lot of code duplication.
======== my solution ========
After figuring out how to login in a request test, thanks to Paul Fioravanti's answer,
before do
post sessions_path, email: user.email, password: user.password
cookies[:remember_token] = user.remember_token
end
I changed all tests to request tests. So I don't have to split them into different files. Paul's solution would also work though I think this is cleaner.
describe 'authorization' do
describe 'as un-signed-in user' do
let(:user) { FactoryGirl.create(:user) }
describe 'getting user edit page' do
before { get edit_user_path(user) }
specify { response.should redirect_to(signin_path) }
end
describe 'putting to user update page' do
before { put user_path(user) }
specify { response.should redirect_to(signin_path) }
end
end
describe 'as wrong user' do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) { FactoryGirl.create(:user, email: 'wrong#example.com') }
before do
post sessions_path, email: user.email, password: user.password
cookies[:remember_token] = user.remember_token
end
describe 'getting user edit page' do
before { get edit_user_path(wrong_user) }
specify { response.should redirect_to(root_path) }
end
describe 'putting to user update page' do
before { put user_path(wrong_user) }
specify { response.should redirect_to(root_path) }
end
end
end
I ended up going through the arduous process of splitting up my request and feature specs after I finished The Rails Tutorial and upgraded my Sample App to Capybara 2.0. Since you say you're still currently doing the tutorial, I would advise you to just keep with the gems that Hartl specifies (Capybara 1.1.2), finish your Sample App, and then go back to the requests/features issue as a refactoring exercise. For your reference though, this is how I ended up writing my "wrong user" authorization specs:
spec/support/utilities.rb
def sign_in_through_ui(user)
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign In"
end
def sign_in_request(user)
post session_path(email: user.email, password: user.password)
cookies[:remember_token] = user.remember_token
end
RSpec::Matchers::define :have_title do |text|
match do |page|
Capybara.string(page.body).has_selector?('title', text: text)
end
end
spec/features/authentication_pages_spec.rb
describe "Authentication on UI" do
subject { page }
# ...
describe "authorization" do
# ...
context "as a wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong#example.com") }
before do
visit root_path
click_link "Sign In"
sign_in_through_ui(user)
end
context "visiting Users#edit" do
let(:page_title) { full_title("Edit User") }
before { visit edit_user_path(wrong_user) }
it { should_not have_title(page_title) }
end
end
end
end
spec/requests/authentication_requests_spec.rb
describe "Authentication Requests" do
subject { response }
# ...
describe "authorization" do
# ...
context "as a wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong#example.com") }
before { sign_in_request(user) }
context "PUT Users#update" do
before { put user_path(wrong_user) }
it { should redirect_to(root_url) }
end
end
end
end
I primarily used the following two links as reference when trying to figure out how to separate my feature specs from my request specs:
rspec-rails and capybara 2.0: what you need to know
rspec-rails Capybara page
Update:
If you don't want the custom RSpec matcher, you can also use the following in the tests above to get the same result on the title element:
its(:source) { should have_selector('title', text: page_title) }
According to Jnicklas (https://github.com/jnicklas/capybara) you should move all Capybare specs you have in spec/requests to spec/features, since spec/features will now be used by Capybara 2.x. So this means that once you moved your Capybara specs to features, you could completely remove these specs from the spec/requests directory.
Personally, I've finished the Ruby on Rails tutorial with no problems at all. I used Capybara 2.x and never used spec/features (just the 'old' spec/requests). For Rspec 2.x support you have to add require >'capybara/rspec'< to your spec_helper.rb file. Without it, your tests could fail.
Edit:
I've just read trough the Rspec docs. If you are using Capybara in your specs these specs have to be moved to spec/features. If there is no Capybara involved the specs can simply stay in your requests directory.
Feature specs
https://www.relishapp.com/rspec/rspec-rails/v/2-12-2/docs/feature-specs/feature-spec!
Request specs
https://www.relishapp.com/rspec/rspec-rails/v/2-12-2/docs/request-specs
More info, from Rubydoc:
http://rubydoc.info/github/jnicklas/capybara/master#Using_Capybara_with_RSpec

Stuck on ROR tutorial CH 9 Ex. 9

I am stuck and cannot figure out where I am going wrong. I have found similar topics but no solutions to these errors:
1) User pages delete links as an admin user admin visiting index page
Failure/Error: it { should have_link('delete', href: user_path(User.first)) }
expected link "delete" to return something
# ./spec/requests/user_pages_spec.rb:127:in `block (5 levels) in <top (required)>'
2) User pages delete links as an admin user admin visiting index page should be able to delete another user
Failure/Error: expect { click_link('delete') }.to change(User, :count).by(-1)
Capybara::ElementNotFound:
no link with title, id or text 'delete' found
# (eval):2:in `click_link'
# ./spec/requests/user_pages_spec.rb:129:in `block (6 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:129:in `block (5 levels) in <top (required)>'
Test Code
describe "delete links" do
it { should_not have_link('delete') }
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) }
before do
sign_in admin
visit users_path
end
it { should have_link('delete', href: user_path(User.first)) }
it "should be able to delete another user" do
expect { click_link('delete') }.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(admin)) }
end
end
Controller:
def
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to users_url
end
Index:
<% provide(:title, 'All users') %>
<h1>All users</h1>
<%= will_paginate %>
<ul class="users">
<%= render #users %>
</ul>
<%= will_paginate %>
Partial
<li>
<%= gravatar_for user, size: 52 %>
<%= link_to user.name, user %>
<% if current_user.admin? && !current_user?(user) %>
| <%= link_to "delete", user, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
</li>
Any guidance/help would be greatly appreciated....
Maybe I came one year later, but I just stated to learn Ruby on Rails from the same tutorial (Rails tutorial from Michael Hartl) and face on the same problem.
Here what I found and how can I did to fix (thanks to our mates commets, I went through the page source code and found the problem.):
From the tutorial, Listing 9.43 says:
require 'spec_helper'
describe "User pages" do
subject { page }
describe "index" do
let(:user) { FactoryGirl.create(:user) }
before do
sign_in user
visit users_path
end
it { should have_title('All users') }
it { should have_content('All users') }
describe "pagination" do
.
.
.
end
describe "delete links" do
it { should_not have_link('delete') }
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) }
before do
sign_in admin
visit users_path
end
it { should have_link('delete', href: user_path(User.first)) }
it "should be able to delete another user" do
expect do
click_link('delete', match: :first)
end.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(admin)) }
end
end end . . . end
Check how the section related to "delete links" is after the "pagination" section.
The pagination explanation starts in Listing 9.33, and has the next code:
require 'spec_helper'
describe "User pages" do
subject { page }
describe "index" do
let(:user) { FactoryGirl.create(:user) }
before(:each) do
sign_in user
visit users_path
end
it { should have_title('All users') }
it { should have_content('All users') }
describe "pagination" do
before(:all) { 30.times { FactoryGirl.create(:user) } }
after(:all) { User.delete_all }
it { should have_selector('div.pagination') }
it "should list each user" do
User.paginate(page: 1).each do |user|
expect(page).to have_selector('li', text: user.name)
end
end
end end . . . end
I realize that the after(:all) { User.delete_all } statement erase ALL records from the test db after of that section (let me continue, because this is so obvious :P ). When the RSpec test continues to "delete links" there are any user created.
The next step is to create a new 'admin' user, and is the only one in the test DB at this point. As is expected the:
it { should have_link('delete', href: user_path(User.first)) }
will be fail because the current user can't delete itself, and it not has any 'delete' link. In the same way the next test will be fail too:
click_link('delete', match: :first)
The solution (Workaround or w/e :D ):
I believe there are some ways to resolve this, I think in two (no. 1 is the one that I used)
A. I add a non-admin user, before the admin user try to login, this let me have 2 users into the test DB, as the follow code:
describe "delete links" do
it { should_not have_link('delete') }
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) }
before do
FactoryGirl.create(:user, name: "test", email: "test#example.com")
sign_in admin
visit users_path
end
it { should have_link('delete', href: user_path(User.first)) }
it "should be able to delete another user" do
expect do
click_link('delete', match: :first)
end.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(admin)) }
end end
The statement FactoryGirl.create(:user, name: "test", email: "test#example.com") did the work.
B. Move all the "delete links" section before the "pagination" section, because I guess in that point, exists at least the first user created in the begin of the *user_pages_spec.rb*.
That's it, with this, I can get green the "delete links" new feature.
my two cents.
iVieL.
The first fixed test is right there ignore the rest. If your solution looks like the below you are golden. The second idea of moving the entire block above the pagination stuff also works.
describe 'delete links' do
it { should_not have_link('delete') }
describe 'as an admin user' do
let(:admin) { FactoryGirl.create(:admin) }
before do
FactoryGirl.create (:user)
sign_in admin
visit users_path
end
it { should have_link('delete', href: user_path(User.first)) }
it 'should be able to delete another user' do
expect { click_link('delete') }.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(admin)) }
end
end
end

Resources