Hi Ronny, Your explanation is very helpful. :-)
Thanks. On Thu, Jan 20, 2011 at 6:19 PM, Ronny Haryanto <[email protected]> wrote: > On Thu, Jan 20, 2011 at 4:33 PM, Joshua Partogi <[email protected]> wrote: >> Can anyone explain what are the cases when to use fixtures/factory >> girl over mock framework? Or can we use mock framework without the >> need to use fixtures/factory girl these days? > > Hi Joshua, > > I use both fixtures and mocks/stubs for testing in my apps. Fixtures > created with Machinist or Factory Girl are very useful for writing > specs or test cases for creating instances of the class under test, we > don't want to stub or mock stuff in the class under test too much, > otherwise we're not really testing it. > > When we're mocking or stubbing we are assuming that the methods or > classes we're stubbing already work and well tested. > > I'll try to illustrate it with some code below. > > class Robot > def pick_object(o) > end > end > > # rspec > describe Robot do > it "picks up object" do > # robot here is a real instance of Robot, so we can test the > actual behaviour of Robot instead of assuming anything > robot = Robot.make # I'm using Machinist here > # While mock_object here can be mocked because it's not the class > under test, and we can assume > # that it's already working and well tested. > mock_object = mock("some object") > # When we call the following, the code in the Robot#pick_object > method will actually be executed. > robot.pick_object(mock_object).should ... > # But if we had robot = mock(Robot, :pick_object => true) above, > then we're assuming that Robot already works > # (which doesn't make sense since we're testing it here) and any > call to pick_object would do nothing > # (the code in Robot#pick_object would not be executed) > # and always returns true > end > end > > So it's still handy to be able to use both mocks/stubs and fixtures in > the above. > > Hope that helps. > > Ronny > > -- > You received this message because you are subscribed to the Google Groups > "Ruby or Rails Oceania" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/rails-oceania?hl=en. > > -- http://twitter.com/jpartogi -- You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en.
