I can''t figure out why this is failing. It seems to have a hard time
assigning the @account. I have spent way too long trying to figure this
out.
Thanks for any help.
-------
NoMethodError in ''StaffMembersController GET index should allow
anonymous users''
You have a nil object when you didn''t expect it!
The error occurred while evaluating nil.staff_members
-------
++++++++++++++++
describe StaffMembersController do
describe "GET index" do
before :each do
@account = mock_model(Account, :staff_members =>
[mock_model(StaffMember)])
controller.stub!(:find_account_by_subdomain_or_url).and_return(@account)
end
# Authorization
it "should allow anonymous users" do
get :index
response.should render_template(:index)
end
end
end
++++++++++++++++
StaffMembersController
++++++++++++++++
class StaffMembersController < ApplicationController
before_filter :find_account_by_subdomain_or_url
def index
@staff_members = @account.staff_members
end
end
+++++++++++++++
--
Posted via http://www.ruby-forum.com/.
I''m guessing the key is inside "find_account_by_subdomain_or_url". Do you have that defined somewhere? It probably calls something like: @account = Account.find_by_subdomain(blah) or Account.find_by_url(blah) In which case that''s probably what you need to stub. Nothing is actually checking for the return value of the controller method or assigning it to @account. In other words, you probably should do Account.stub! instead of controller.stub! Check what''s in that method and see if that gets you closer to the solution. Good luck! Glenn On Apr 7, 2008, at 5:14 PM, chris anderson wrote:> I can''t figure out why this is failing. It seems to have a hard time > assigning the @account. I have spent way too long trying to figure > this > out. > > Thanks for any help. > > ------- > NoMethodError in ''StaffMembersController GET index should allow > anonymous users'' > You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.staff_members > ------- > > ++++++++++++++++ > describe StaffMembersController do > > describe "GET index" do > > before :each do > @account = mock_model(Account, :staff_members => > [mock_model(StaffMember)]) > controller.stub! > (:find_account_by_subdomain_or_url).and_return(@account) > end > > # Authorization > it "should allow anonymous users" do > get :index > response.should render_template(:index) > end > > end > end > ++++++++++++++++ > > StaffMembersController > ++++++++++++++++ > class StaffMembersController < ApplicationController > before_filter :find_account_by_subdomain_or_url > > def index > @staff_members = @account.staff_members > end > > end > +++++++++++++++ > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Glenn Ford wrote:> I''m guessing the key is inside "find_account_by_subdomain_or_url". Do > you have that defined somewhere? It probably calls something like: > > @account = Account.find_by_subdomain(blah) or Account.find_by_url(blah) > > In which case that''s probably what you need to stub. Nothing is > actually checking for the return value of the controller method or > assigning it to @account. In other words, you probably should do > Account.stub! instead of controller.stub! Check what''s in that method > and see if that gets you closer to the solution. > > Good luck! > GlennThat worked. I was under the impression that there would be no subdomain available when testing, which is why I was stubbing the controller method. After it working I actually checked what the request.domain was and saw that it is test.host :) Thanks Glenn -- Posted via http://www.ruby-forum.com/.