Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As far as I can see your tables are not being cleared after the tests are run. Are you sure your tests clear the table at exit? Usually the Database Cleaner gem is used to do this.</p> <p>The problem appears to be with this line of code:</p> <pre><code>@user = Factory(:user) </code></pre> <p>in this part of your spec:</p> <pre><code>describe "authentication of edit/update pages" do before(:each) do @user = Factory(:user) end </code></pre> <p>Try changing it to</p> <pre><code>@user = Factory(:user, :email =&gt; "foo@bar.com") </code></pre> <p>Or remove it completely.</p> <p>It seems to me that it's nested under this parent spec:</p> <pre><code>describe "PUT 'update'" do before(:each) do @user = Factory(:user) test_sign_in(@user) end </code></pre> <p>And as here a user model with the same email is created before running the <code>"authentication of edit/update pages"</code> specs, when in <code>"authentication of edit/update pages"</code> you create the user the spec fails because <code>email</code> should be unique.</p> <p><strong>UPDATE:</strong></p> <p>I had a look at your spec and looks like you had forgotten to close a block in the right place (and <code>end</code> keyword was misplaced).</p> <p>This is the correct spec:</p> <pre><code>require 'spec_helper' describe UsersController do render_views describe "GET 'index'" do describe "for non-signed-in users" do it "should deny access" do get :index response.should redirect_to(signin_path) flash[:notice].should =~ /sign in/i end end describe "for signed-in users" do before(:each) do @user = test_sign_in(Factory(:user)) second = Factory(:user, :name =&gt; "Bob", :email =&gt; "another@example.com") third = Factory(:user, :name =&gt; "Ben", :email =&gt; "another@example.net") @users = [@user, second, third] 30.times do @users &lt;&lt; Factory(:user, :name =&gt; Factory.next(:name), :email =&gt; Factory.next(:email)) end end it "should be successful" do get :index response.should be_success end it "should have the right title" do get :index response.should have_selector("title", :content =&gt; "All users") end it "should have an element for each user" do get :index @users[0..2].each do |user| response.should have_selector("li", :content =&gt; user.name) end end it "should paginate users" do get :index response.should have_selector("div.pagination") response.should have_selector("span.disabled", :content =&gt; "Previous") response.should have_selector("a", :href =&gt; "/users?page=2", :content =&gt; "2") response.should have_selector("a", :href =&gt; "/users?page=2", :content =&gt; "Next") end end end describe "GET 'show'" do before(:each) do @user = Factory(:user) end it "should be successful" do get :show, :id =&gt; @user response.should be_success end it "should find the right user" do get :show, :id =&gt; @user assigns(:user).should == @user end it "should have the right title" do get :show, :id =&gt; @user response.should have_selector("title", :content =&gt; @user.name) end it "should include the user's name" do get :show, :id =&gt; @user response.should have_selector("h1", :content =&gt; @user.name) end it "should have a profile image" do get :show, :id =&gt; @user response.should have_selector("h1&gt;img", :class =&gt; "gravatar") end end describe "GET 'new'" do it "should be successful" do get 'new' response.should be_success end it "should have the right title" do get 'new' response.should have_selector("title", :content =&gt; "Sign up") end it "should have a name field" do get :new response.should have_selector("input[name='user[name]'][type='text']") end it "should have an email field" do get :new response.should have_selector("input[name='user[email]'][type='text']") end it "should have a password field" do get :new response.should have_selector("input[name='user[password]'][type='password']") end it "should have a password confirmation field" do get :new response.should have_selector("input[name='user[password_confirmation]'][type='password']") end end describe "POST 'create'" do describe "failure" do before(:each) do @attr = {:name =&gt; "", :email =&gt; "", :password =&gt; "", :password_confirmation =&gt; ""} end it "should not create a user" do lambda do post :create, :user =&gt; @attr end.should_not change(User, :count) end it "should have the right title" do post :create, :user =&gt; @attr response.should have_selector("title", :content =&gt; "Sign up") end it "should render the 'new' page" do post :create, :user =&gt; @attr response.should render_template('new') end end describe "success" do before(:each) do @attr = {:name =&gt; "New User", :email =&gt; "user@example.com", :password =&gt; "foobar", :password_confirmation =&gt; "foobar"} end it "should create a user" do lambda do post :create, :user =&gt; @attr end.should change(User, :count).by(1) end it "should sign the user in" do post :create, :user =&gt; @attr controller.should be_signed_in end it "should redirect to the user show page" do post :create, :user =&gt; @attr response.should redirect_to(user_path(assigns(:user))) end it "should have a welcome message" do post :create, :user =&gt; @attr flash[:success].should =~ /welcome to the sample app/i end end end describe "GET 'edit'" do before(:each) do @user = Factory(:user) test_sign_in(@user) end it "should be successful" do get :edit, :id =&gt; @user response.should be_success end it "should have the right title" do get :edit, :id =&gt; @user response.should have_selector("title", :content =&gt; "Edit user") end it "should have a link to change the Gravatar" do get :edit, :id =&gt; @user gravatar_url = "http://gravatar.com/emails" response.should have_selector("a", :href =&gt; gravatar_url, :content =&gt; "change") end end describe "PUT 'update'" do before(:each) do @user = Factory(:user) test_sign_in(@user) end describe "failure" do before(:each) do @attr = {:email =&gt; "", :name =&gt; "", :password =&gt; "", :password_confirmation =&gt; ""} end it "should render the 'edit' page" do put :update, :id =&gt; @user, :user =&gt; @attr response.should render_template('edit') end it "should have the right title" do put :update, :id =&gt; @user, :user =&gt; @attr response.should have_selector("title", :content =&gt; "Edit user") end end describe "success" do before(:each) do @attr = {:name =&gt; "New Name", :email =&gt; "user@example.org", :password =&gt; "barbaz", :password_confirmation =&gt; "barbaz"} end it "should change the user's attributes" do put :update, :id =&gt; @user, :user =&gt; @attr @user.reload @user.name.should == @attr[:name] @user.email.should == @attr[:email] end it "should redirect to the user show page" do put :update, :id =&gt; @user, :user =&gt; @attr response.should redirect_to(user_path(@user)) end it "should have a flash message" do put :update, :id =&gt; @user, :user =&gt; @attr flash[:success].should =~ /updated/ end end end describe "authentication of edit/update pages" do before(:each) do @user = Factory(:user) end describe "for non-signed-in users" do it "should deny access to 'edit'" do get :edit, :id =&gt; @user response.should redirect_to(signin_path) end it "should deny access to 'update'" do put :update, :id =&gt; @user, :user =&gt; {} response.should redirect_to(signin_path) end end describe "for signed-in users" do before(:each) do wrong_user = Factory(:user, :email =&gt; "user@example.net") test_sign_in(wrong_user) end it "should require matching users for 'edit'" do get :edit, :id =&gt; @user response.should redirect_to(root_path) end it "should require matching users for 'update'" do put :update, :id =&gt; @user, :user =&gt; {} response.should redirect_to(root_path) end end end describe "DELETE 'destroy'" do before(:each) do @user = Factory(:user) end describe "as a non-signed-in user" do it "should deny access" do delete :destroy, :id =&gt; @user response.should redirect_to(signin_path) end end describe "as a non-admin user" do it "should protect the page" do test_sign_in(@user) delete :destroy, :id =&gt; @user response.should redirect_to(root_path) end end describe "as an admin user" do before(:each) do admin = Factory(:user, :email =&gt; "admin@example.com", :admin =&gt; true) test_sign_in(admin) end it "should destroy the user" do lambda do delete :destroy, :id =&gt; @user end.should change(User, :count).by(-1) end it "should redirect to the users page" do delete :destroy, :id =&gt; @user response.should redirect_to(users_path) end end end end </code></pre> <p>However in this corrected spec, the <code>UsersController DELETE 'destroy' as a non-signed-in user should deny access</code> spec fails, because the implementation is incorrect (it assumes <code>current_user</code> will never return <code>nil</code> but it sometimes does).</p> <p><strong>UPDATE 2:</strong> All the changes that are necessary to make all the tests pass can be seen <a href="https://gist.github.com/1744655" rel="nofollow">here</a>.</p> <p>Other than indentation fixes the only thing that needed to be fixed was the <code>admin_user</code> callback in the <code>UsersController</code> class:</p> <pre><code> def admin_user redirect_to(root_path) unless (current_user &amp;&amp; current_user.admin?) end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload