【问题标题】:Following Rails Tutorial and a syntax error is stopping a test passing遵循 Rails 教程和语法错误正在停止测试通过
【发布时间】:2017-01-18 16:38:24
【问题描述】:

不幸的是,我收到了一个隐藏的语法错误。有人可以帮忙吗?我知道它与 div 的“assert_select”方法有关,但我不确定为什么它没有通过。

我的错误:

Error:
UsersSignupTest#test_invalid_signup_information:
Nokogiri::CSS::SyntaxError: unexpected '#' after '[#<Nokogiri::CSS::Node:0x0055b3736715e8 @type=:ELEMENT_NAME, @value=["div"]>]'
    test/integration/users_signup_test.rb:15:in `block in <class:UsersSignupTest>'

我的测试:

require 'test_helper'

class UsersSignupTest < ActionDispatch::IntegrationTest
    test "invalid signup information" do
        get signup_path
        assert_no_difference 'User.count' do
            post users_path, params: { user: {
                name: "",
                email: "user@invalid",
                password: "foo",
                password_confirmation: "bar"
                }}
            end
            assert_template 'users/new'
            assert_select 'div#<CSS id for error explanation>'
            assert_select 'div.<CSS class for field with error>'
    end

它正在测试的代码。用户/新用户:

<% provide(:title, "sign up") %>
<h1>Sign up</h1>

<div class="row">
    <div class="col-md-6 col-md-offset-3">
        <%= form_for(@user, url: signup_path) do |f| %>
            <%= render 'shared/error_messages' %>
            <%= f.label :name %>
            <%= f.text_field :name, class: "form-control" %>

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

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

            <%= f.label :password_confirmation, "Confirmation" %>
            <%= f. password_field :password_confirmation, class: "form-control" %>

            <%= f.submit "Create my account", class: "btn btn-primary" %>

            <% end %>

        </div>
    </div>

共享/_error_messages:

<% if @user.errors.any? %>
    <div id="error_explanation">
        <div class="alert alert-danger">
            The form contains <%= pluralize(@user.errors.count, "error") %>.
        </div>
      <ul>
        <% @user.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
        <% end %>
      </ul>
     </div>
<% end %>

【问题讨论】:

    标签: ruby-on-rails ruby testing minitest railstutorial.org


    【解决方案1】:

    您正在学习 Hartl rails 教程,因此您复制并粘贴了信息或忘记填写信息。

    在第 15 行,您有 'div#&lt;CSS id for error explanation&gt;' 它应该是用于错误解释的 CSS...'div#error_explanation' 和以下 'div.field_with_errors'

    【讨论】:

      【解决方案2】:

      在 assert_select 中,您要求它转到 CSS 中的“div” 所以你只需要写它的名字

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-17
        相关资源
        最近更新 更多