Rspec没有登录我 [英] Rspec not logging me in
问题描述
我正在查看此答案,以了解如何测试会话控制器并编写如下内容:
I was looking at this answer to see how to test a session controller and wrote something like this:
require 'spec_helper'
describe SessionsController do
context "We should login to the system and create a session" do
let :credentials do
{:user_name => "MyString", :password => "someSimpleP{ass}"}
end
let :user do
FactoryGirl.create(:user, credentials)
end
before :each do
post :create , credentials
end
it "should create a session" do
puts user.inspect
puts session[:user_id]
#session[:user_id].should == user.id
end
end
end
基于此,我创建了一个工厂女用户:
Based on that I created a factory girl user:
FactoryGirl.define do
factory :user, :class => 'User' do
name "sample_user"
email "MyString@gmail.com"
user_name "MyString"
password "someSimpleP{ass}"
end
end
现在一切正常-before :each do
语句的摘录-它从不登录"用户",因此-我无法测试其控制器功能,是否正确创建了会话?
Now it all works - exceot for the before :each do
statement - it never "logs" the "user" in - thus I cannot test the controllers functionality of, is a session properly created?
现在大多数人会说,使用capybara并通过这种方式对其进行测试-但这是错误的,IMO-确定我是否正在进行可以正常工作的前端测试,但是我正在测试基于控制器的逻辑.有人可以告诉我为什么这不起作用吗?路由工作正常.
Now most would say, use capybara and test it through that way - but that's wrong, IMO - sure if I'm doing front end testing that would work, but I'm testing controller based logic. Can some one tell me why this isn't working? routing works fine.
我的puts session[:user_id]
快到零了,
推荐答案
您误解了SessionsController和RegistrationsController.
You misunderstood SessionsController and RegistrationsController.
会话适用于已经注册的用户,而不是用于创建用户.在SessionController中的#create表示创建会话,而不是用户.
A Session is for an user who has already registered, not for creating an user. #create in SessionController means to create a session, not an user.
RegistrationController用于创建具有完整详细信息的用户,包括password_confirmation.
RegistrationController is for creating user with full details including password_confirmation.
要测试SessionsController,首先需要在FactoryGirl中创建一个有效用户,然后使用其凭据(例如电子邮件和密码)登录.
To test SessionsController, you need to create a valid user in FactoryGirl at first, then use his credentials say email and password to sign in.
这篇关于Rspec没有登录我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!