Ruby在测试中返回nil类,在dev

时间:2016-07-01 03:11:04

标签: ruby testing railstutorial.org

我试图使用rails而不是外部gem来实现登录系统,并且一直在关注Michael Hartl教程,这些教程最明显地突然出现。到目前为止,该网站本身运行良好,它是2个测试与登录我挣扎:

require 'test_helper'

class UsersLoginTest < ActionDispatch::IntegrationTest

    def setup
      @user = users(:michael)
    end

    test "login with invalid information" do
      get login_path
      assert_template 'sessions/new'
      post login_path, params: { session: { email: "", password: "" } }
      assert_template 'sessions/new'
      assert_not flash.empty?
      get root_path
      assert flash.empty?
    end

    test "login with valid information" do
      get login_path
      post login_path, params: { session: { email:    @user.email,
                                      password: 'password' } }
      assert_redirected_to @user
      follow_redirect!
      assert_template 'users/show'
      assert_select "a[href=?]", login_path, count: 0
      assert_select "a[href=?]", logout_path
      assert_select "a[href=?]", user_path(@user)
    end

  end

我的错误消息是:

ERROR["test_login_with_invalid_information", UsersLoginTest, 2016-06-30 22:30:36 +0100]
test_login_with_invalid_information#UsersLoginTest (1467322236.17s)
  NoMethodError:         NoMethodError: undefined method `[]' for nil:NilClass
        app/controllers/sessions_controller.rb:7:in `create'
        test/integration/users_login_test.rb:12:in `block in <class:UsersLoginTest>'
    app/controllers/sessions_controller.rb:7:in `create'
    test/integration/users_login_test.rb:12:in `block in <class:UsersLoginTest>'

  ERROR["test_login_with_valid_information", UsersLoginTest, 2016-06-30 22:30:36 +0100]
   test_login_with_valid_information#UsersLoginTest (1467322236.18s)
  NoMethodError:         NoMethodError: undefined method `[]' for nil:NilClass
        app/controllers/sessions_controller.rb:7:in `create'
        test/integration/users_login_test.rb:21:in `block in       <class:UsersLoginTest>'
    app/controllers/sessions_controller.rb:7:in `create'
    test/integration/users_login_test.rb:21:in `block in       <class:UsersLoginTest>'

错误代码指向以下控制器:

  class SessionsController < ApplicationController

    def new
    end

    def create
      user = User.find_by(email: params[:session][:email].downcase)
      if user && user.authenticate(params[:session][:password])
        log_in user
        redirect_to user
      else
        flash.now[:danger] = 'Invalid email/password combination'
        render 'new'
      end
    end

    def destroy
    end
  end

我在开始时假设这是因为会话辅助方法不可用,但是站点本身在开发模式下运行良好并且可以登录。我错过了会话​​助手或测试文件本身的内容吗?我的会议助手:

module SessionsHelper

  # Logs in the given user.
  def log_in(user)
    session[:user_id] = user.id
  end

  # Returns the current logged-in user (if any).
  def current_user
    @current_user ||= User.find_by(id: session[:user_id])
  end

  # Returns true if the user is logged in, false otherwise.
  def logged_in?
    !current_user.nil?
  end
end

最后我的应用程序控制器:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  include SessionsHelper
end

为文本墙道歉并提前致谢

1 个答案:

答案 0 :(得分:0)

我想如果你改变了这个

var webdriver = require('selenium-webdriver')
    , By = webdriver.By
    , until = webdriver.until;
var driver = new webdriver.Builder().forBrowser('firefox').build();
driver.get('http://www.vapeworld.com/');
driver.manage().timeouts().implicitlyWait(10, 10000);
var hrefs = driver.findElements(webdriver.By.tagName("a"));
hrefs.then(function (elements) {
    elements.forEach(function (element) {
        element.getAttribute('name').then(function (obj) {
            if (obj == '1_name') {
                console.log(obj);
                element.click();
            }
        });
    });
});

进入这个

post login_path, params: { session: { email: "", password: "" } }

在你的第一次测试中,它会通过。如果有的话,尝试使用相同的策略。如果失败了,我建议在开始时在控制器中创建一个post login_path, session: { email: "", password: "" } 创建方法,运行测试以检查参数,然后根据您学到的内容确定解决方案。