ruby-on-rails-3 – Rails 3 RSpec和assert_select

前端之家收集整理的这篇文章主要介绍了ruby-on-rails-3 – Rails 3 RSpec和assert_select前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想过尝试使用Rspec.但是我得到了assert_select的下一个问题.
1) UserController login page open login page contains all expected controls
Failure/Error: assert_select "form[action=?]","/user/login" do MiniTest::Assertion:
Expected at least 1 element matching "form[action='/user/login']",found 0.
# (eval):2:in `assert'
# ./spec/controllers/user_controller_spec.rb:20:in `block (3 levels) in <top (required)>'

这是我的代码

describe UserController do
  describe "login page open" do
    it "login page contains all expected controls" do
      get :login
      assert_select "form[action=?]","/user/login" do
      assert_select "input[name=?]","username"
      assert_select "input[name=?]","password"
      assert_select "input[type=?]","submit"
    end
  end
end

当我在浏览器中打开一个登录页面时,这个页面打开没有问题.

解决方法

默认情况下,RSpec(至少在较新版本)中,当您运行控制器规格规格时,会阻止Rails呈现视图.他们希望您以视图规格测试您的观点,而不是控制器规格.由于视图不呈现,assert_select始终失败.

但是对于像我这样的人来说,想要在他们的控制器规范中测试偶尔的视图片段,他们提供了一个render_views方法.你必须在你的描述或上下文块中调用它,而不是在它的块内.

describe UserController do

  render_views       # <== ADD THIS

  describe "login page open" do
    it "login page contains all expected controls" do
      get :login
      assert_select "form[action=?]","submit"
    end
  end
end
原文链接:https://www.f2er.com/ruby/266242.html

猜你在找的Ruby相关文章