[mocha-developer] Organizing tests and mocha expectations

Bryan Helmkamp bhelmkamp at gmail.com
Sun Oct 8 19:12:56 EDT 2006


I'm simultaneously getting into using Mocha and RSpec-style tests
(courtesy of the simply_bdd plugin) and I'm struggling with some
issues while trying to organize my specs/test.  Here's a code example
illustrating the problem:

context "update cliient invalid data" do
  include ClientsControllerSpecHelper

  specify "should render edit form" do
    setup_controller

    @client = mock
    Client.expects(:find).with("1").returns(@client)

    @client.expects(:attributes=).with("attributes")
    @client.expects(:save!).raises(ActiveRecord::RecordInvalid.new(stub(:errors
=> stub(:full_messages => []))))

    @controller.expects(:render).with()
    @controller.expects(:render).with(:action => "edit")\

    put :update, :id => 1, :client => "attributes"
  end
end

Now, to fit well with RSpec's methodology, I'd like to have a setup
method which gets the context ready, and then follow that up with some
specifications.  Essentially here's the logic:

Update client with invalid data
 - Should find client by id
 - Should attempt to save client with new attributes
 - Should render edit form

The problem is that I can't break the expectations up into separate
test methods like that because all the tests depend on those in order
to work.  It's as if each of those expectations is both setup code and
a test, and each test relies on all the setup code for the whole test
case.

Is  there anyway to reorganize this code to still be DRY but yet also
achieve more granular tests, despite their interdependencies?

Thanks,

-Bryan


-- 
http://www.MyCongress.org/ -- coming soon


More information about the mocha-developer mailing list