我设计了在我的Rails应用程序上设置的身份验证和注册。我使用after_sign_in_path_for()
来根据各种情况自定义用户登录时的重定向。如何测试after_sign_in_path_for(资源)?
我在问的是如何测试这种方法?这似乎很难隔离,因为它会自动通过设计,当用户SIGNES在叫我想做的事情是这样的:
describe ApplicationController do
describe "after_sign_in_path_for" do
before :each do
@user = Factory :user
@listing = Factory :listing
sign_in @user
end
describe "with listing_id on the session" do
before :each do
session[:listing_id] = @listing.id
end
describe "and a user in one team" do
it "should save the listing from the session" do
expect {
ApplicationController.new.after_sign_in_path_for(@user)
}.to change(ListingStore, :count).by(1)
end
it "should return the path to the users team page" do
ApplicationController.new.after_sign_in_path_for(@user).should eq team_path(@user.team)
end
end
end
end
end
但是这显然不是这样做,因为我刚刚得到一个错误的方式:
Failure/Error: ApplicationController.new.after_sign_in_path_for(@user)
RuntimeError:
ActionController::Metal#session delegated to @_request.session, but @_request is nil: #<ApplicationController:0x00000104581c68 @_routes=nil, @_action_has_layout=true, @_view_context_class=nil, @_headers={"Content-Type"=>"text/html"}, @_status=200, @_request=nil, @_response=nil>
那么,我该如何测试这种方法呢?
非常好!我不知道它是如何工作的,但它的工作! –
谢谢!你刚刚救了我另一个设计头痛 –
谢谢你太多。我正在考虑这个问题,但对这个controller.after_sign_in_path_for(@user)没有任何想法。作业==作业_路径 – Ricbermo