[rspec-users] Mocks? Really?

David Chelimsky dchelimsky at gmail.com
Thu Dec 6 12:04:42 EST 2007


On Dec 6, 2007 10:56 AM, Andy Goundry <andy at adveho.net> wrote:
> 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!(:id).and_return(1)
>   @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')
>   end
>
> This is handy and keeps the view test isolated from changes to your
> models, but is that really the point?

It depends on what you value. If you are doing BDD, then you are
running all of your examples between every change. If you are doing
that, you value fast-running examples.

>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

This is matter of mindset. In my view, the view is still behaving as
expected, it's just that the expectation is wrong. What's not behaving
correctly is the application, which, as you point out, we would learn
from stories (integration tests).

HTH,
David

>
> Thanks
>
> Andy
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list