Ruby-on-rails – What’s the difference between mock, stub, and factory girl

factory-botmockingrspecruby-on-rails

I'm pretty new to rspec and the whole TDD methodology. Can someone please explain the difference between mock and stub. When do we use them and when do we use Factory Girl to create objects in test cases?

Best Answer

You could think of a mock (or double) as a fake object. When you're testing and need to work with an object that isn't easily usable inside your test, you could use a mock as an approximation for how you expect that object to behave and work around it. Stubs can be used in a similar manner but on an individual method on an object.

Here's a rather contrived example of using a lot of both:

class Client
  def connect_to_server
    if Server.connect.status == 'bad'
      show_an_error
    else
      do_something_else
    end
  end
  def do_something_else; end
  def show_an_error; end
end

context "failure" do
  it "displays an error" do
    bad_network_response = double("A bad response from some service", :status => 'bad')
    Server.should_receive(:connect).and_return(bad_network_response)

    client = Client.new
    client.should_receive(:show_an_error)
    client.connect_to_server
  end
end

You can imagine that using a lot of mocks or stubbing is a bad idea; this is basically masking out parts of your code in your test but, it's an easy solution for some difficult/rare testing scenarios.

Factory Girl is useful for generating data for tests. You would use factories as recipes for creating instances for your models, you might need to test something involving a lot of test data and this would be a situation where using fixtures won't work and creating complicated objects explicitly can be tedious.