I'm adding more controllers to the admin section of the Padrino but I can't workout how to stub the current user or a session with Factory Girl or Mocha.
What is a good way for testing controller actions that need a current session?
I'm adding more controllers to the admin section of the Padrino but I can't workout how to stub the current user or a session with Factory Girl or Mocha.
What is a good way for testing controller actions that need a current session?
这个解决方案似乎有效
def set_current_user(user)
ApplicationController.stub(:current_user).and_return(user)
session[:identity_id] = user.id
end
警告:我没有使用过帕德里诺,你也没有给出任何你尝试过的代码,所以这是相当笼统和模糊的。
不要存根会话,而是使用像 Capybara 这样为您设置 cookie jar 的测试框架。将RSpec shared_context与运行登录的块before
一起使用。after
我不记得 Capybara 的确切语法,我会让你去查一下,但它会是这样的:
shared_context "When logged in" do
before do
visit "/login"
fill_in "username", user.name
fill_in "password", user.password
click "login!"
end
after do
# log out…
end
end
describe "Something that you need to be logged in for" do
let(:user) { OpenStruct.new({name: "blah", password: "blurgh" }) }
include "When logged in"
before do
visit "/only/authenticated/see/this"
end
subject { page }
it { should be_ok }
it { #… }
end
使用Rack::Test
,看看这个答案
这是身份验证助手,因此您应该存根logged_in?
返回true
并current_account
返回用户双精度(无论是来自 FactoryGirl 还是来自其他let
地方)。这样您的应用就不会从会话中询问信息。