Currently, with rspec-rails (2.14.2), I test my associations in model specs with the shoulda (3.5.0) gem like so:
# app/models/user.rb
class User < ActiveRecord::Base
belongs_to :school
end
# spec/models/user_spec.rb
describe User do
it { should belong_to :school }
end
After some research, I hit a wall trying to make association-related assertions work (they all seem to fail).
Error message:
1) User
Failure/Error: it { is_expected.to belong_to :school }
NoMethodError:
undefined method `belong_to' for #<RSpec::ExampleGroups::School:0x007f8a4c7a68d0>
# ./spec/models/user.rb:4:in `block (2 levels) in <top (required)>'
So my questions are:
shoulda-matchers is the gem that provides association, validation, and other matchers.
The shoulda gem (which we also make) is for using shoulda-matchers with Test::Unit (and also provides some nice things like contexts and the ability to use strings as test names). But if you're on RSpec, you'll want to use shoulda-matchers, not shoulda.
This is the way it works, now with the shoulda-matchers gem and the "expect" syntax
describe User, type: :model do
it { is_expected.to belong_to :school }
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With