rspec/rspec-rails — Issue #500: rspec.should have(:some).associations
Author: wdiechmann
Created: 2012-02-06 09:22:04+00:00
State: open

On [SO](http://stackoverflow.com/questions/9155046/why-does-validation-not-kick-in-when-run-in-rails-console-when-in-fact-it-does-w) I've asked why active record validations only kicks in when running rspec from command line, not when 'proxying' the test in RAILS_ENV=test r c

I'm in no way full of Rspec-fu but something about associations does not seem right! Example:

describe UserGroup do

setup :make_ox

def make_ox
begin
@ox = Ox.find(1)
rescue ActiveRecord::RecordNotFound
@ox = Factory.create(:ox, id: 1)
end
end

it { should have_many(:roleables) }
it { should have_many(:roles).through(:roleables) }

it "should have one role" do
role = Role.create(name: "role", ox: @ox)
user_group = UserGroup.create(name: "UG1", ox: @ox)
permission = Roleable.create(ox: @ox, authorization: "-", role: role, roleable: user_group)

user_group.roleables << permission
user_group.should be_valid
role.should be_valid
permission.should be_valid
permission.roleable.should eq user_group
user_group.roleables[0].should eq permission
user_group.should have(1).roleable
user_group.should have(1).roles # <- this line makes the test fail (none of the above)

end
end