0
我有以下的RSpec測試:控制器是在無RSpec的測試
require 'rails_helper'
require 'spec_helper'
RSpec.describe "Users", type: :request do
describe "sign in/out" do
describe "success" do
it "should sign a user in and out" do
attr = {:name=>"Test1",
:email => "[email protected]",
:password => "foobar",
:password_confirmation => "foobar"
}
user = User.create(attr)
visit signin_path
fill_in "Email", :with => user.email
fill_in "Password", :with => user.password
puts page.body
click_button "Sign in"
controller.should be_signed_in
click_link "Sign out"
controller.should_not be_signed_in
end
end
end
end
我收到以下錯誤:
Failure/Error: controller.should be_signed_in
expected to respond to `signed_in?
這是因爲controller
是nil
。這裏有什麼不對,導致controller
爲nil
?
Controller類是:
class SessionsController < ApplicationController
def new
@title = "Sign in"
end
def create
user = User.authenticate(params[:session][:email],
params[:session][:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Sign in"
render 'new'
else
sign_in user
redirect_to user
end
end
def destroy
sign_out
redirect_to root_path
end
end
signed_in
方法,其中包括會話輔助限定。
Ruby平臺信息: 紅寶石:2.0.0p643 的Rails 4.2.1 RSpec的:3.2.2
我將此測試用例移至控制器測試用例。 – doptimusprime