Matt,
Thank you very much. It turns out that a pair of
authentication/authorization before_filters were interfering with the test.
I got past it now by doing this:
before(:each) do
@user = mock_user
@login_params = { :login => 'quentin', :password => 'monkey' }
User.stub!(:authenticate).with(@login_params[:login],
@login_params[:password]).and_return(@user)
@user.stub!(:enabled?).and_return(true)
@user.stub!(:account_id).and_return(1)
@user.stub!(:time_zone).and_return('Eastern Time (US & Canada)')
@user.stub!(:role).and_return('normal')
User.stub!(:in_role?).with('limited').and_return(false)
controller_bypass_authentication(@user)
end
I've got 3 roles in my app (admin, normal, and limited), in case you were
curious about that. I didn't know about the hash trick. Thanks!
- Mark
On Wed, Jan 7, 2009 at 6:36 AM, Matt Wynne <[email protected]> wrote:
> On 7 Jan 2009, at 01:25, Mark A. Richman wrote:
>
> I am trying to get the following test to pass, and get this error. Since
>> I'm only on day 4 of rspec, I'm sure I'm missing something simple. Any
>> ideas?
>> ## rake spec
>>
>> 'PatientsController GET 'new' should be successful' FAILED
>>
>> expected success? to return true, got false
>> ./spec/controllers/patients_controller_spec.rb:27:
>>
>> ## patients_controller_spec.rb
>>
>> require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
>>
>>
>> describe PatientsController do
>>
>> integrate_views
>> fixtures :all
>>
>> before(:each) do
>> @user = mock_user
>> @login_params = { :login => 'quentin', :password => 'monkey' }
>>
>> User.stub!(:authenticate).with(@login_params[:login],
>> @login_params[:password]).and_return(@user)
>> @user.stub!(:enabled?).and_return(true)
>> @user.stub!(:account_id).and_return(1)
>> @user.stub!(:time_zone).and_return('Eastern Time (US & Canada)')
>>
>> @user.stub!(:role).and_return('Normal')
>> controller_bypass_authentication(@user)
>> end
>>
>> #Delete these examples and add some real ones
>> it "should use PatientsController" do
>>
>> controller.should be_an_instance_of(PatientsController)
>> end
>>
>> describe "GET 'new'" do
>> it "should be successful" do
>> get 'new'
>> response.should be_success # this is the offending line 27
>>
>> end
>> end
>> end
>>
>> # Allows a spec to bypass auth for controllers that filter for logged_in
>> user
>> def controller_bypass_authentication(user=nil)
>> controller.stub!(:logged_in).and_return(true)
>> controller.stub!(:authorized?).and_return(true)
>>
>> controller.stub!(:current_user).and_return(user)
>> end
>>
>> ## patients_controller.rb
>>
>> ...
>> def new
>> @patient = Patient.new
>>
>> respond_to do |format|
>> format.html # new.html.erb
>> format.xml { render :xml => @patient }
>>
>> end
>> end
>> ...
>> http://www.pastie.org/354313
>>
>
> I don't think there's enough to go on here. We need to know why the request
> failed, and it could be for any number of reasons. You've shown us a load of
> test setup code for stubbing out your authentication mechanism, but you
> haven't shown us the actual authentication mechanism in your controller, so
> we have no way of knowing whether your stubs are good.
>
> I would start your debugging by adding a puts statement between lines 26
> and 27 to show more of the (unexpected) response. Looking at response.code
> or response.body should give you some clues.
>
> By the way, did you know you can pass hashes of stubs to the mock() method?
>
> You could clean up your setup code quite a bit by doing this:
>
> @user = mock(User,
> :enabled? => true,
> :account_id => 1,
> :time_zone => 'Eastern Time (US & Canada)',
> :role => 'Normal')
>
> HTH,
>
> Matt Wynne
> http://blog.mattwynne.net
> http://www.songkick.com
>
> _______________________________________________
> rspec-users mailing list
> [email protected]
> http://rubyforge.org/mailman/listinfo/rspec-users
>
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users