Stubbing class in controller

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

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:

Thanks Ivo,

I was getting a bit confused with the stubs, thanks for the explanation.

Jamie

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs