[rspec-users] Stubbing class in controller

Jamie D jam5t3r.lists at gmail.com
Sat Apr 19 02:23:12 EDT 2008


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


More information about the rspec-users mailing list