Note that there are some explanatory texts on larger screens.

plurals
  1. PORails Tutorial Chapter 10 RSpec Failures by the end of chapter
    primarykey
    data
    text
    <p>I'm working on the Rails tutorial and as follow the instructions, I always get the results expected but after finishing <a href="http://ruby.railstutorial.org/chapters/user-microposts#top" rel="nofollow">chapter 10</a>, I get the bellow errors and I can't find what it the issue is.</p> <pre><code>1) UsersController PUT 'update' authentication of edit/update pages for non-signed-in users should deny access to 'edit' Failure/Error: @user = Factory(:user) ActiveRecord::RecordInvalid: Validation failed: Email has already been taken # ./spec/controllers/users_controller_spec.rb:259:in `block (4 levels) in &lt;top (required)&gt;' 2) UsersController PUT 'update' authentication of edit/update pages for non-signed-in users should deny access to 'update' Failure/Error: @user = Factory(:user) ActiveRecord::RecordInvalid: Validation failed: Email has already been taken # ./spec/controllers/users_controller_spec.rb:259:in `block (4 levels) in &lt;top (required)&gt;' 3) UsersController PUT 'update' authentication of edit/update pages for signed-in users should require matching users for 'edit' Failure/Error: @user = Factory(:user) ActiveRecord::RecordInvalid: Validation failed: Email has already been taken # ./spec/controllers/users_controller_spec.rb:259:in `block (4 levels) in &lt;top (required)&gt;' 4) UsersController PUT 'update' authentication of edit/update pages for signed-in users should require matching users for 'update' Failure/Error: @user = Factory(:user) ActiveRecord::RecordInvalid: Validation failed: Email has already been taken # ./spec/controllers/users_controller_spec.rb:259:in `block (4 levels) in &lt;top (required)&gt;' 5) UsersController DELETE 'destroy' as a non-signed-in user should deny access Failure/Error: delete :destroy, :id =&gt; @user NoMethodError: undefined method `admin?' for nil:NilClass # ./app/controllers/users_controller.rb:68:in `admin_user' # ./spec/controllers/users_controller_spec.rb:303:in `block (4 levels) in &lt;top (required)&gt;' </code></pre> <p>the app runs well and the functionalities are done but the tests don't pass. Initially I thought there might be typos and then I double checked to see if I missed something but there was not luck...</p> <p>For reference this is my RSpec test:</p> <pre><code>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 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 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 </code></pre> <p>And my user controller methods:</p> <pre><code>def update @user = User.find(params[:id]) if @user.update_attributes(params[:user]) flash[:success] = "Profile updated." redirect_to @user else @title = "Edit user" render 'edit' end end def destroy User.find(params[:id]).destroy flash[:success] = "User destroyed." redirect_to users_path end </code></pre> <p>Here is my factories.rb</p> <pre><code># By using the symbol ':user', we get Factory Girl to simulate the User model. Factory.define :user do |user| user.name "Michael Hartl" user.email "mhartl@example.com" user.password "foobar" user.password_confirmation "foobar" end Factory.sequence :name do |n| "Person #{n}" end Factory.sequence :email do |n| "person-#{n}@example.com" end </code></pre> <p>Any help would be GREATLY appreciated!!</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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