[rspec-users] Stubbing class in controller
ivo.dancet at gmail.com
Sat Apr 19 04:22:15 EDT 2008
The 'loadavg' method is an instance method of 'whm', so the test
@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.
Op 19-apr-08, om 08:23 heeft Jamie D het volgende geschreven:
> 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
> and my test code:
> before(:each) do
> @server = mock_model(Server)
> def do_get
> get :show, :id => "1"
> it "should be successful" do
> response.should be_success
> After running my tests I get a failure message:
> 'ServersController handling GET /servers/1 should be successful'
> expected success? to return true, got false
> I'm not really sure how to stub this kind of class call.
> rspec-users mailing list
> rspec-users at rubyforge.org
More information about the rspec-users