Jim Mitchener
Jim Mitchener

Reputation: 9003

Mock external class in Rails controller spec

I have a Rails 3 app that I am testing with RSpec. I have a controller using external class MustMock as

class FooController < ApplicationController
  def myaction
    mockme = MustMock.new
    @foobar = mockme.do_something
  end
end

How can I best mock the instance of MustMock in my controller spec?

Upvotes: 2

Views: 585

Answers (2)

Mori
Mori

Reputation: 27799

describe FooController do
  specify :myaction do
    MustMock.should_receive(:new)
            .and_return(stub :do_something => :something)
    get :myaction
    assigns[:foobar].should == :something
  end
end

Upvotes: 5

Finbarr
Finbarr

Reputation: 32206

You could try this:

it "does something in myaction" do
    my_stub = stub()
    my_stub.should_receive(:do_something).once.and_return(:foobar)
    MustMock.stub(:new => my_stub)
    get :myaction
    response.should be_success
end

Upvotes: 2

Related Questions