[rspec-users] Mocking classes

Marcus Roberts M.Roberts at NITTYGRITTY.NET
Fri Feb 27 12:02:50 EST 2009



>This is not an rspec bug :)

I thought not :-)

>This is assigning the value mock("MachineInstance") to the constant
>MachineInstance, which an interesting way to approach stubbing a
>class, but not really the idea.

>Can you show us the spec this is in so we can make appropriate recommendations?

We have a class declared in lib/ in a Rails app which we want to test independently of the activerecord class / database. The full spec as it stands is:

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe "a service at level 0" do

  it "should pass a test and do nothing" do

    service = mock("service") 
    service.should_receive(:test_service)
    service.should_receive(:save)
  
    machineInstance = mock("MachineInstance")
    machineInstance.should_receive(:services).and_return([service])
    
    MachineInstance = mock("MachineInstance")
    MachineInstance.should_receive(:find).and_return([machineInstance])
    
    monitoringAlerts = MonitoringAlerts.new
    monitoringAlerts.monitor
  end
end


The idea is to have no dependency on the MachineInstance class in this spec, which seems to work, but specs called later still see MachineInstance as the mock.

Are we doing this completely the wrong way?

Thanks!

Marcus
-------------- next part --------------

The WatchGuard Firebox that protects your network has detected a message that may not be safe.

Cause : The file type may not be safe.
Content type : application/ms-tnef
File name    : winmail.dat
Virus status : No information.
Action       : The Firebox deleted winmail.dat.

Your network administrator can not restore this attachment.



More information about the rspec-users mailing list