Rails在学习Rails教程的同时测试错误和失败

时间:2019-02-11 23:19:05

标签: ruby-on-rails

我目前正在学习Rails教程。在Rails测试后,TDD,我有错误消息。还有更多消息,但是首先我要调试这些消息。我有登录问题。即使可以注册,也无法使用电子邮件和密码登录。

rails测试错误日志

 1) Failure:
UsersLoginTest#test_login_with_valid_information_followed_by_logout [/home/ec2-user/environment/sample_app/test/integration/users_login_test.rb:26]:
Expected exactly 0 elements matching "a[href="/login"]", found 1..
Expected: 0
  Actual: 1

test / integration / users_login_test.rb

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 followed by logout" do
    get login_path
    post login_path, params: { session: { email:    @user.email,
                                          password: 'password' } }
    assert is_logged_in?
    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)

    delete logout_path
    assert_not is_logged_in?
    assert_redirected_to root_url
    delete logout_path
    follow_redirect!
    assert_select "a[href=?]", login_path
    assert_select "a[href=?]", logout_path,      count: 0
    assert_select "a[href=?]", user_path(@user), count: 0

  end

  test "login with remembering" do
    log_in_as(@user, remember_me: '1')
    assert_not_empty cookies['remember_token']
  end

  test "login without remembering" do
    log_in_as(@user, remember_me: '1')
    delete logout_path
    log_in_as(@user, remember_me: '0')
    assert_empty cookies['remember_token']
  end
end

app / controllers / users_controller.rb

class UsersController < ApplicationController
  before_action :logged_in_user, only:[:index, :edit,:update]
  before_action :correct_user, only:[:edit, :update]

  def index
    @users = User.paginate(page: params[:page])
  end

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
   @user = User.new(user_params)
    if @user.save
      log_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

   def edit
     @user = User.find(params[:id])
   end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

    private

    def user_params
      params.require(:user).permit(
      :name, :email, :password, 
      :password_confirmation)
    end

    def logged_in_user
      unless logged_in?
        store_location
        flash[:danger] = "Please log in."
        redirect_to login_url
      end
    end

    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless current_user?(@user)
    end
end

app / views / sessions / new.html.erb

<% provide(:title, "Log in") %>
<h1>Log in</h1>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(:session, url:login_path) do |f| %>

      <%= f.label :email %>
      <%= f.email_field :email, class:'form-control' %>

      <%= f.label :password %>
      <%= f.password_field :password, class:'form-control' %>

      <%= f.label :remember_me, class: "checkbox inline" do %>
        <%= f.check_box :remember_me %>
        <span>Remember me on this computer</span>
      <% end %>

      <%= f.submit "Log in", class:"btn btn-primary" %>
    <% end %>

      <p>New user? <%= link_to "Sign up now!", signup_path %></p>
  </div>
</div>

app / views / layouts / _header.html.erb

<header class="navbar navbar-fixed-top navbar-inverse">
  <div class="container">
    <%= link_to "sample app", root_path, id: "logo" %>
    <nav>
      <ul class="nav navbar-nav navbar-right">
        <li><%= link_to "Home", root_path %></li>
        <li><%= link_to "Help", help_path %></li>
        <% if logged_in? %>
          <li><%= link_to "Users", users_path %></li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
              Account <b class="caret"></b>
            </a>
            <ul class="dropdown-menu">
              <li><%= link_to "Profile", current_user %></li>
              <li><%= link_to "Settings", edit_user_path(current_user) %></li>
              <li class="divider"></li>
              <li>
                <%= link_to "Log out", logout_path, method: :delete %>
              </li>
            </ul>
          </li>
        <% else %>
          <li><%= link_to "Log in", login_path %></li>
        <% end %>
      </ul>
    </nav>
  </div>
</header>

Rails服务器的错误日志

Started POST "/login" for 160.86.161.32 at 2019-02-12 00:16:21 +0000
Cannot render console from 160.86.161.32! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
   (0.1ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"JWDGUASZpEIXXSnesdlgoB1dY0vDoZRPKT+lgAaKVinOe07SQpTRwyFsDX2D7QHOzSx1UWxAqe6elhMPQu719Q==", "session"=>{"email"=>"gonnbe1106@yahoo.co.jp", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."email" = ? LIMIT ?  [["email", "gonnbe1106@yahoo.co.jp"], ["LIMIT", 1]]
   (0.1ms)  begin transaction
   (0.1ms)  commit transaction
Redirected to https://06f94f09794143fa9d708cbff698cf78.vfs.cloud9.us-east-2.amazonaws.com/users
Completed 302 Found in 99ms (ActiveRecord: 4.5ms)


Started GET "/users" for 160.86.161.32 at 2019-02-12 00:16:22 +0000
Cannot render console from 160.86.161.32! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by UsersController#index as HTML
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = NULL LIMIT ?  [["LIMIT", 1]]
Redirected to https://06f94f09794143fa9d708cbff698cf78.vfs.cloud9.us-east-2.amazonaws.com/login
Filter chain halted as :logged_in_user rendered or redirected
Completed 302 Found in 3ms (ActiveRecord: 0.4ms)


Started GET "/login" for 160.86.161.32 at 2019-02-12 00:16:22 +0000
Cannot render console from 160.86.161.32! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by SessionsController#new as HTML
  Rendering sessions/new.html.erb within layouts/application
  Rendered sessions/new.html.erb within layouts/application (502.7ms)
  Rendered layouts/_shim.html.erb (0.3ms)
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = NULL LIMIT ?  [["LIMIT", 1]]
  Rendered layouts/_header.html.erb (2.5ms)
  Rendered layouts/_footer.html.erb (0.4ms)
Completed 200 OK in 724ms (Views: 722.9ms | ActiveRecord: 0.2ms)

app / views / sessions / new.html.erb

<% provide(:title, "Log in") %>
<h1>Log in</h1>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(:session, url: login_path) do |f| %>
      <%= f.label :email %>
      <%= f.email_field :email, class: 'form-control' %>

      <%= f.label :password %>
      <%= f.password_field :password, class: 'form-control' %>

      <%= f.label :remember_me, class: "checkbox inline" do %>
        <%= f.check_box :remember_me %>
        <span>Remember me on this computer</span>
      <% end %>

      <%= f.submit "Log in", class: "btn btn-primary" %>
    <% end %>

    <p>New user? <%= link_to "Sign up now!", signup_path %></p>
  </div>
</div>

app / controllers / sessions_controller.rb

class SessionsController < ApplicationController

  # GET /login
  def new
    #@session = Session.new
  end

  # POST /login 
  def create
    user = User.find_by(email: params[:session][:email])
    if user && user.authenticate(params[:session][:password])
      # Success
      log_in user
      params[:session][:remember_me] == '1' ? remember(user) : forget(user)
      redirect_back_or user
    else
      # Failure
      flash.now[:danger] = 'Invalid email/password combination'
      render 'new'
    end
  end

  # DELETE /logout
  def destroy
    log_out if logged_in?
    redirect_to root_url
  end
end

我想把这种失败变成断言。你能帮我吗?

0 个答案:

没有答案