ruby-on-railsrubyruby-mocha

Difference between RR mock.instance_of and Mocha any_instance


I have the following rspec code:

require 'spec_helper'
require 'mocha'
require 'rr'

describe ProjectsController, "creating a new project" do
  integrate_views

  it "should redirect to project with a notice on successful save" do
    Project.any_instance.stubs(:valid?).returns(true)
    #mock.instance_of(Project).valid? {true}
    Project.any_instance.stubs(:create_default_packets)
    #mock.instance_of(Project).create_default_packets
    post 'create'
    assigns[:project].should_not be_new_record
  end
end

It passes successfully as written (RR syntax commented out) but when I switch to the RR syntax it fails with:

'ProjectsController creating a new project should redirect to project with a notice on successful save' FAILED expected new_record? to return false, got true

What is the difference between the two that RR would fail?


Solution

  • It appears the difference is that you can't call instance_of twice with rr.

    EDIT: this may no longer be the case, see https://github.com/rr/rr#class-instances