method

mock

mock(name, stubs_and_options={})
public

Shortcut for creating an instance of Spec::Mocks::Mock.

name is used for failure reporting, so you should use the role that the mock is playing in the example.

stubs_and_options lets you assign options and stub values at the same time. The only option available is :null_object. Anything else is treated as a stub value.

Examples

  stub_thing = mock("thing", :a => "A")
  stub_thing.a == "A" => true

  stub_person = stub("thing", :name => "Joe", :email => "[email protected]")
  stub_person.name => "Joe"
  stub_person.email => "[email protected]"

1Note

A typical usage for a mock

hosiawak ยท Jun 4, 20092 thanks

You want to use a mock when you're testing a behaviour of one of your methods that interacts with some outside world service (eg. an FTP server).

it "should login to ftp server" do ftp = mock('Ftp server', :null_object => true) Net::FTP.should_receive(:new).and_return(ftp) ftp.should_receive(:login).with('username', 'password') some_obj.connect end

def connect session = Net::FTP.new('server.com') session.login('username', 'password') session.close end