Rails测试模板呈现错误

时间:2017-09-07 16:57:59

标签: ruby-on-rails testing

在我一直在做的课程中,我接受了一项家庭作业,对用户进行了整合测试。注册页面。这是我的工作:

require 'test_helper'

class SignupTest < ActionDispatch::IntegrationTest

  test "create a new user" do
    get '/signup'
    assert_template '/signup'
    assert_difference 'User.count',1 do
      post :create,user:{username: "jay",email:"jay_lann@gmail.com",password: "american"}
      assert_redirected_to user_path(assigns(:user))
    end
    assert_template "users/#{:user.id}"
  end
end

然而,当我rake test文件时会弹出错误。它会抛出此错误:

Failure:
SignupTest#test_create_a_new_user [c:/Users/dadi/Desktop/project1/app/test/integration/signup_test.rb:7]:
expecting <"/signup"> but rendering with <["shared/_errors", "users/_form", "users/new", "layouts/_navigation", "layouts/_messages", "layouts/_footer", "layouts/application"]>

我已经看到了这个错误的一些解决方案,但是没有一个能够正常工作。非常感谢帮助。非常感谢!

(由于我在测试中并不精通,指出我所犯的任何错误但尚未弹出但对我来说意义重大。)

编辑:我已经尝试过您的解决方案。我的代码现在看起来像这样:

require 'test_helper'

class SignupTest < ActionDispatch::IntegrationTest

  test "create a new user" do
    get '/signup'

    assert_difference 'User.count',1 do
      post :create,user:{username:"jay",email:"jay_lann@gmail.com",password: "american"}
      assert_redirected_to user_path(assigns(:user))
    end
    assert_template "users/#{:user.id}"
  end
end

但是,当我rake test文件时,我收到此错误:

Error:
SignupTest#test_create_a_new_user:
URI::InvalidURIError: bad URI(is not URI?): http://www.example.com:80create
    test/integration/signup_test.rb:9:in `block (2 levels) in <class:SignupTest>
'
    test/integration/signup_test.rb:8:in `block in <class:SignupTest>'

错误在第8行和第9行,但我无法弄清楚错误(我知道无效的URI代码,但我无法弄清楚我在哪里制作了一个URI误差)。

0 个答案:

没有答案