我想过尝试使用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