How do you mock a push into a class array variable using Rspec? Here is an over-simplified example:
class Foo
attr_accessor :bar
def initialize
@bar = []
end
end
def some_method(foo)
foo.bar << "a"
end
Say I want to write a spec for some_method that “it should push a new value to bar”. How do I do that?
foo = Foo.new
foo.should_receive(WHAT GOES HERE???).with("a")
some_method(foo)
Why mock anything? You only need to mock something when it is a dependency that you are trying to isolate from what you are actually trying to test. In your case, you seem to be trying to verify that calling
some_methodresults in adding an item to a property of an object you passed in. You can simply test it directly:* EDITED After Comments Below **