[rspec-users] Checking that a controller created a separate model object

O. Frabjous-Dey O.Frabjous.Dey at gmail.com
Sun Oct 12 22:33:33 EDT 2008


Hi everyone, RSpec newbie here.  I'm looking forward to interacting with the
community as I learn more about TDD, RSpec, Rails, and... TDD through RSpec
in Rails.

Having watched the Peepcode screencasts and read a lot of documentation, I'm
trying to write my first comprehensive applications using TDD from the
ground up.  I'm stuck writing a test for my controller, though.

This application is your basic crappy social network.  There are three
relevant models in play right now: User, Group, and Membership.  User and
Group have :has_many relationships with each other :through Membership;
Membership, in addition to having a user_id and a group_id, also has a
column called "rank", to which I write in either "officer" or "member" as a
value.

Group also defines the following relationships just for convenience:

  has_many :memberships
  has_many :officers, :through => :memberships, :source => :user,
:conditions => "rank = 'officer'"
  has_many :members, :through => :memberships, :source => :user, :conditions
=> "rank = 'member'"

I'm trying to test the CREATE functionality in my Group controller.
Unfortunately, this test is not working.

      it "should make the creating user an officer" do
        Group.stub!(:new).and_return(mock_group(:save => true))
        post :create, :group => {}
        assigns[:group].should have(1).officer
      end

The error message:
  Mock 'Group_1008' received unexpected message :officer with (no args)

If I understand this correctly, I am getting the error because
assigns[:group] isn't really an ActiveRecord object, just a mock, and I
guess the relationships don't carry over?  If this is the case, what's the
right way to test it, assuming that a Membership really is being created in
my controller's create function?

Thanks,
O.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081012/8d3b230e/attachment.html>


More information about the rspec-users mailing list