[rspec-users] Mocks? Really?

Andy Goundry andy at adveho.net
Thu Dec 6 11:56:31 EST 2007

OK, so i've played a bit with mocks and mock_models in controller and
view tests and i have a question. Is this statement really correct:

"We highly recommend that you exploit the mock framework here rather
than providing real model objects in order to keep the view specs
isolated from changes to your models."
(http://rspec.rubyforge.org/documentation/rails/writing/views.html in
section 'assigns')

I ask because this wonderful declaration passes in my view test even
though the model Project doesn't have the field 'synopsis' yet:

  @project1 = mock_model(Project)
  @project1.stub!(:name).and_return("My first project")
  @project1.stub!(:synopsis).and_return("This is a fantastic new project")
  assigns[:projects] = [@project1]

  it "should show the list of projects with name and synopsis" do
    render "/projects/index.rhtml"
    response.should have_tag('div#project_1_name', 'My first project')
    response.should have_tag('div#project_1_synopsis', 'This is a
fantastic new project')
    response.should have_tag('div#project_2_name', 'My second project')
    response.should have_tag('div#project_2_synopsis', 'This is
another fantastic project')
    response.should have_tag('a', 'This is another fantastic project')

This is handy and keeps the view test isolated from changes to your
models, but is that really the point? What if someone later changes
the model and updates the model tests so that they pass but do not
realize that they've then broken the view?

I'm sure i am simply missing the point here or not taking into account
integration testing that would i expect aim to catch such changes, but
somehow i want my view tests to tell me that the views are no longer
going to behave as expected



More information about the rspec-users mailing list