I can't get my tests to work and was wondering if anyone have any pointers. I am testing my user edit page such that you have to be logged in to be able to see and edit the user profile. Following the authlogic documentation, here are the relevant codes:
我不能让我的测试工作,我想知道是否有人有任何指示。我正在测试我的用户编辑页面,以便您必须登录才能查看和编辑用户配置文件。在authlogic文档之后,以下是相关代码:
class ApplicationController < ActionController::Base
helper_method :current_user_session, :current_user
private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
def require_current_user
unless current_user
flash[:error] = 'You must be logged in'
redirect_to sign_in_path
end
end
end
class UsersController < ApplicationController
before_filter :require_current_user, :only => [:edit, :update]
...
end
In my users_controller_spec
describe "GET 'edit'" do
before(:each) do
@user = Factory(:user)
UserSession.create(@user)
end
it "should be successful" do
# test for /users/id/edit
get :edit, :id => @user.id
response.should be_success
end
I tested it using the browser and it works. You have to be logged in to be able to edit your profile but it doesn't work in my rspec test.
我用浏览器测试了它,它是有效的。您必须登录才能编辑您的配置文件,但它在我的rspec测试中不起作用。
I have a feeling it has something to do with mock controller but I can't figure out how. I've also read the test case but still can't get it to work.
我有一种感觉,它和模拟控制器有关,但我不知道怎么做。我也读过这个测试用例,但是仍然不能正常工作。
Please help and much thanks!!!
请帮忙,谢谢!!!
1 个解决方案
#1
2
You can stub the current_user method on the ApplicationController like so:
您可以在应用程序控制器上存根current_user方法,如下所示:
fake_user = controller.stub(:current_user).and_return(@user) #you could use a let(:user) { FactoryGirl.create(:user) } instead of before_each
get :edit, :id => fake_user.id
response.should be_success
#1
2
You can stub the current_user method on the ApplicationController like so:
您可以在应用程序控制器上存根current_user方法,如下所示:
fake_user = controller.stub(:current_user).and_return(@user) #you could use a let(:user) { FactoryGirl.create(:user) } instead of before_each
get :edit, :id => fake_user.id
response.should be_success