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)
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With