I’m trying to test a controller to ensure that only an authorized party can view the correct child object using RSpec. I cant figure out what I’m doing wrong as I’m getting this error:
ActiveRecord::RecordInvalid: Validation failed: Company can't be blank
I have a Plan object and a Company object. The Store can have many plans (think of a pest control Company). I want to test that given a known scenario I can retrieve the plan fo the Company (assuming there is only one).
The Plan looks like this:
class Plan < ActiveRecord::Base
before_save :default_values
# Validation
validates :amount, :presence => true
validates :company, :presence => true
# Plans belong to a particular company.
belongs_to :company, :autosave => true
scope :find_all_plans_for_company, lambda {
|company| where(:company_id => company.id)
}
# Other code ...
end
The Company looks like this:
class Company < ActiveRecord::Base
validates :name, :presence => true
validates :phone1, :presence => true
validates_format_of :phone1, :phone2,
:with => /^[\(\)0-9\- \+\.]{10,20}$/,
:message => "Invalid phone number, must be 10 digits. e.g. - 415-555-1212",
:allow_blank => true,
:allow_nil => true
has_many :users
has_many :plans
end
.. controller looks like this
def index
@plans = Plan.find_all_plans_for_company(current_user.company)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @plans }
end
end
.. and my RSpec test looks like this (excuse me if its full of gimmickery, I’m just splunking around with it and cannot get it to work).
describe PlansController do
def valid_attributes
{
:company_id => 1,
:amount => 1000
}
end
describe "GET index" do
it "should return the Plans for which this users company has" do
@company = mock_model(Company, :id => 1, :name => "Test Company", :phone1 => "555-121-1212")
Company.stub(:find).with(@company.id).and_return(@company)
controller.stub_chain(:current_user, :company).and_return(@company)
plan = Plan.create! valid_attributes
get :index, {}
assigns(:plans).should eq([plan])
end
# Other tests ...
end
end
The problem is, when I try this (or any of the crazy other variants I’ve tried) I get this error:
ActiveRecord::RecordInvalid: Validation failed: Company can't be blank
I’m not sure why this is happening as I thought the Company.stub call would handle this for me. But apparently not.
What am I missing here and what am I doing wrong? How can I get this test to pass?
Let’s peel back the layers on this spec, to make sure things make sense (and to make sure I understand what’s going on). First, what are you testing?
So you want to check that the plans associated with the company of the current user are assigned to
@plans. We can stub or mock out everything else.Looking at the controller code, we have:
What do we need to get this to work, without hitting the database and without depending on the models?
First of all, we want to get a mock
companyout ofcurrent_user.company. This is what these two lines in your spec code do:This will cause
current_user.companyto return the mock model@company. So far so good.Now to the class method
find_all_plans_for_company. This is where I’m a bit confused. In your spec, you stub thefindmethod onCompanyto return@companyforid = 1.But really, wouldn’t it suffice just to do something like this in your controller code?:
If you did it this way, then in your test you could just mock a plan, and then return it as the
plansassociation for your mock company:Then the assignment should work, and you don’t need to actually create any model or hit the database. You don’t even need to give your mock company an id or any other attributes, which anyway are irrelevant to the spec.
Maybe I’m missing something here, if so please let me know.