[rspec-users] Stubbing class in controller

Ivo Dancet ivo.dancet at gmail.com
Sat Apr 19 04:22:15 EDT 2008


Hi

The 'loadavg' method is an instance method of 'whm', so the test  
should be:

@whm = mock_model(Whm::Client)
Whm::Client.stub!(:new).and_return(@whm) # make the new method to  
return a mock object
@whm.stub!(:loadavg).and_return(3) # 3 is the dummy loadavg, maybe you  
can change it in something more meaningfull

For now, there is no real reason to put this into the test itself. I  
think I would put this code in the before part.

Regards
Ivo

Op 19-apr-08, om 08:23 heeft Jamie D het volgende geschreven:

> Hi,
>
> I am trying to stub a class in my controller and I can't get it to
> work, the controller code is:
>
>  def show
>    @server = Server.find(params[:id])
>    whm = Whm::Client.new @server
>    @server_load = whm.loadavg
>  end
>
> and my test code:
>
>    before(:each) do
>      @server = mock_model(Server)
>      Server.stub!(:find).and_return(@server)
>    end
>
>    def do_get
>      get :show, :id => "1"
>    end
>
>    it "should be successful" do
>      Whm::Client.stub!(:new)
>      Whm::Client.stub!(:loadavg)
>      do_get
>      response.should be_success
>    end
>
> After running my tests I get a failure message:
>
> 'ServersController handling GET /servers/1 should be successful'  
> FAILED
> expected success? to return true, got false
> ./spec/controllers/servers_controller_spec.rb:63:
>
> I'm not really sure how to stub this kind of class call.
>
> Thanks
> Jamie
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list