Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Stubbing ability when testing views with RSpec? #602

Closed
tborisova opened this Issue Apr 16, 2012 · 5 comments

Comments

Projects
None yet
3 participants

Hello, I am writing rspec tests for testing that a user with role "user" doesn't see the link "Edit User" so that it cannot edit other users but I get the error undefined method `authenticate' for nil:NilClass.
I have this code:

before(:each) do
 @user_b = FactoryGirl.create(:user, :email=>"foo@bar.bg", :password=>"132132132",
                             :password_confirmation=>"132132132", :id=>34534578687, :role=>"user")
end

describe "Edit link" do
  it "should see it if the page shows the profile of the current user" do   
    render
    rendered.should have_content(@user.email)
    rendered.should have_content(@user.role)
    rendered.should have_selector("a",:href=>"users/#{@user.id}/edit",:value=>"Edit")
  end                           
end

and this is in shared_example and then in context "user is with role 'user'" I have this:

@user = FactoryGirl.create(:user,:email=>"foo33@bar.bg",:password=>"132132132",    
                                        :password_confirmation=>"132132132",:id=>34534578686,:role=>"user")
            
ApplicationController.stub(:current_user).and_return(@user)

but I get this error:

 Failure/Error: render
     ActionView::Template::Error:
       undefined method `authenticate' for nil:NilClass

I tried the things that are in here: https://github.com/ryanb/cancan/wiki/Testing-Abilities but nothing worked. Has someone faced the same problem?

Thanks in advance.

Update: Formatted the code and the description.

Collaborator

derekprior commented May 11, 2012

Can you post the entirety of a minimal reproduction for this? It's unclear how you are including the shared example. The pieces you posted aren't enough for me to see what you are trying to do. If you could strip this down to a base case and post that, I may be able to help.

Collaborator

andhapp commented May 13, 2012

Can you please read the comment on another issue and try stubbing the methods out?

#598 (comment)

@andhapp andhapp was assigned May 13, 2012

I have User model generated with devise and I tried:

view.should_receive(:current_user).and_return(@user) and view.should_receive(:current_ability).and_return(Ability.new(@user)) 

but I still get the error:

Failure/Error: render
  ActionView::Template::Error:
  undefined method `authenticate' for nil:NilClass

Here is my code:

require 'spec_helper'

describe "users/show.html.erb" do
    shared_examples_for "user who can manage his profile" do
        it "should see his email" do
            view.should_receive(:current_abillity).and_return(Ability.new(@user))
            view.should_receive(:current_user).and_return(@user)
            
            render
            rendered.should have_content(@user.email)
        end
    end
    
    context "user with role 'user'" do
        before(:each) do
                @user = FactoryGirl.create(:user,:email=>"foo@bar.bg",:password=>"132132132",
                :password_confirmation=>"132132132",:id=>34534578687,:role=>"user")
            end
            
            it_behaves_like "user who can manage his profile"
    end
    

Should I require some gems?

I tried this and it worked!:

require 'spec_helper'

describe "mymodel/index.html.erb" do
  before(:each) do
    assign(:my_model,mock_model(MyModel))
    @ability = Object.new
    @ability.extend(CanCan::Ability)
    controller.stub(:current_ability) { @ability }
  end

  context "authorized user" do
    it "can see the table header" do
      @ability.can :destroy, MyModel
      render
      rendered.should have_selector('th:contains("Options")')
    end
  end

  context "unauthorized user" do
    it "cannot see the table header" do
      render
      rendered.should_not have_selector('th:contains("Options")')
    end
  end
end

@tborisova tborisova closed this May 13, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment