On 2008-10-12, at 22:33, O. Frabjous-Dey wrote:
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.

Hi O. The Group that you're creating is a Mock. Thus, it doesn't have any of the methods (Eg: #officer) that a real Group has. Since #officer hasn't been stubbed out on the mock Group, the error occurs.

As for how to spec that the relationships are being setup correctly, can you provide the code for the Group's "new" action?

Cheers,
Nick
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to