Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you stub all methods of a particular mock instance

I have a particular mock that is being handled by a third party. I just want to check that the same mock has been returned back.

However, the third party calls array methods and save methods that my test doesnt really care about. Is there a way to tell my mock that it expects/stub all methods to do with that particular mock instance?

eg.

user = mock(User)
user.stub_all

Thanks!

EDIT

More info about the problem:

Test:

  it "creating an invitation should return invitation" do
    invitation = mock_model(Invitation)
    invitation.stub(:[]=)
    invitation.stub(:save)
    Invitation.stub(:create).and_return(invitation)
    @user.create_invitation
    @user.create_invitation.should == invitation        
  end

Code being tested:

 def create_invitation
    invitation = Invitation.create
    self.invitations.push(invitation)
    return invitation
  end

I need to mock the following which are not directly related to what I am testing:

invitation.stub(:[]=)
invitation.stub(:save)
like image 236
Karan Avatar asked Apr 23 '12 22:04

Karan


People also ask

What is stub in mock?

Stub: Stub is an object that holds predefined data and uses it to answer calls during tests. Such as: an object that needs to grab some data from the database to respond to a method call. Mocks: Mocks are objects that register calls they receive.

What is stub instance?

Use any_instance.stub on a class to tell any instance of that class to. return a value (or values) in response to a given message. If no instance. receives the message, nothing happens.

What is method stubbing in Java?

A method stub or simply stub in software development is a piece of code used to stand in for some other programming functionality. A stub may simulate the behavior of existing code (such as a procedure on a remote machine; such methods are often called mocks) or be a temporary substitute for yet-to-be-developed code.


1 Answers

The answer is

user = mock(User).as_null_object

but in general this approach means your objects are too large and your tests aren't granular enough

like image 160
Gareth Avatar answered Oct 31 '22 20:10

Gareth