Im still trying to figure out rspec and right now I am using authlogic to handle my users and sessions. I did the usual authlogic stuff like adding the def current_user method to applciation controller, and as a helper method, but how do I access that in my rspec controll开发者_开发问答er file?
Here is my user_sessions_controller_spec.rb:
require 'spec_helper'
require 'authlogic'
describe UserSessionsController do
context "user is already logged in" do
before(:each) do
include Authlogic::TestCase
activate_authlogic
UserSession.create Factory.build(:user)
end
it "should redirect the user to the home page" do
get 'new'
response.should redirect_to(home_path)
end
end
describe "#create" do
context "when the user is not logged in" do
before(:each) do
current_user = nil
end
it "correct authorization should create a new session" do
post 'create', {:login => "afactoryuser", :password => "apass", :password_confirmation => "apass"}
current_user.should_not be_nil
end
end
end
end
when i run rspec it just tells me:
correct authorization should create a new session
undefined local variable or method `current_user' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2::Nested_1:0x000001017d3410>
so im guessing it is in the rspec context...but how do i know it should be in user-sessions_controller? And am I even testing it correctly?
Insted of current_user in your RSpec, try controller.current_user
If you want to stub current_user method use: controller.stub!(:current_user).and_return(whatever)
in before :each
block, but then you will always get whatever
if you stub it.
精彩评论