[rspec-users] Mocking Access Control

David Chelimsky dchelimsky at gmail.com
Tue Jul 24 17:51:49 EDT 2007


On 7/24/07, Justin Williams <carpeaqua at gmail.com> wrote:
> I'm trying to jump on the TDD/BDD bandwagon, but am having trouble
> understanding how i should mock my user.  The user has a habtm
> relationship to a roles model (acl_system2 plugin), but I'm not sure
> how to tell rspec about a model.
>
> My code:
>
> describe UsersController do
>   integrate_views
>
>   before(:each) do
>     @user = mock_model(User)
>     @user.stub!(:new_record?).and_return(false)
>     @user.stub!(:id).and_return(666)
>     @user.stub!(:email).and_return("john at doe.com")
>     @user.stub!(:password).and_return("dummypassword")
>     User.stub!(:new).and_return(@user)
>   end
>
>   it "should login as a tutor" do
>     @user.stub!(:type).and_return("Tutor")
>     post :login, {:login => {:email => "teamup at teamup.host", :password
> => "teamup"}}
>     response.should redirect_to(:controller => "toolkit/overview",
> :action => "index")
>     should_be_logged_in
>   end
>
>   it "should logout" do
>     get :logout
>     response.should redirect_to(:controller => "users", :action => "login")
>     should_not_be_logged_in
>   end
>
>   protected
>
>   def should_be_logged_in
>     response.session.should_not be_nil
>     session[:user].should_not be_nil
>   end
>
>   def should_not_be_logged_in
>     response.session.should_not be_nil
>     session[:user].should be_nil
>   end
> end
>
> Unfortunately, the actual method its testing works fine, but my test
> is getting the following error:
>
> "expected redirect to {:action=>"index",
> :controller=>"toolkit/overview"}, got no redirect"
>
> I am pretty sure it is because I haven't set the role attribute, but
> I'm not sure how to establish that relationship using stubs.  I tried
> setting mock_model(User, :roles => mock(Role, :title => "tutor")) but
> that didn't seem to matter.
>
> Anyone able to shed some light on this for me?

Would you please post the code for the actions as well?

>
> Thanks.
>
> --
> -
> Justin Williams
> work: http://secondgearllc.com/
> play: http://carpeaqua.com
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list