I have two models – Tenant and User, each tenant will have_many users and I am trying to find a way of testing that the ability to create a user and automatically assign tenant. When I try run the test I am getting the following error:
NoMethodError:
undefined method 'reflect_on_association' for Proc:Class
Tenant Code:
class Tenant < ActiveRecord::Base
attr_accessible :name, :billing_email, :country
validates :name, :presence => true,
:length => { :maximum => 75 },
:uniqueness => true
validates :billing_email, :email => true
has_many :users
end
User Code:
class User < ActiveRecord::Base
attr_accessible :email, :first_name, :last_name, :password, :tenant_id
validates :email, :presence => true,
:uniqueness => true,
:email => true
validates :first_name, :presence => true,
:length => { :maximum => 50 }
validates :last_name, :presence => true,
:length => { :maximum => 50 }
validate :password_validation
has_many :sessions, :dependent => :destroy
belongs_to :tenant
end
Test Attempt:
lambda do
@attr = FactoryGirl.attributes_for(:user)
post users_path, :user => @attr
response.should be_success
end.should belong_to(:tenant)
It’s not particularly clear what you’re trying to achieve here. Asserting that one object “belongs_to” another smells bad. Do you really care that belongs_to was used for the association? Too often it seems that rspec tests end up duplicating the code under test without actually testing anything useful.
More likely you’re interested to see if the result of POSTing to your controller produces the correct results. Since you haven’t shown us your controller and since I think RSpec is pointless noise, here’s how I’d tackle this..