Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 控制器在rspec测试中为零_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

Ruby on rails 控制器在rspec测试中为零

Ruby on rails 控制器在rspec测试中为零,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,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", :

我进行了以下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 => "dmishra@test.org",
        :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
。这里有什么问题导致
控制器

控制器类为:

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
class sessioncontroller
signed_in
方法在包含的会话助手中定义

Ruby平台信息: Ruby:2.0.0p643 轨道:4.2.1 RSpec:3.2.2这是一个请求规范(基本上是一个rails集成测试),设计用于跨越多个请求,可能跨越控制器

controller
变量由集成测试提供的请求方法设置(
get
put
post
等)

如果改为使用capybara DSL(访问、单击等),则不会调用集成测试方法,因此
controller
将为零。当使用capybara时,您没有访问单个控制器实例的权限,因此无法测试诸如
signed\u in?
返回的内容-您必须测试更高级别的行为(例如页面上的内容)。

这是一个请求规范(基本上是rails集成测试),旨在跨多个请求,可能跨越控制器

controller
变量由集成测试提供的请求方法设置(
get
put
post
等)


如果改为使用capybara DSL(访问、单击等),则不会调用集成测试方法,因此
controller
将为零。使用capybara时,您没有访问单个控制器实例的权限,因此您无法测试诸如
登录的内容?
返回的内容-您必须测试更高级别的行为(例如页面上的内容)。

我将此测试用例移动到控制器测试用例。我将此测试用例移动到控制器测试用例。