I've thought to try to use Rspec. But I get a next problem with the 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)>'
This is my code snippet
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
When I open a login page in a browser this page opens without problem.
By default, RSpec (at least in newer versions) prevents Rails from rendering views when you run controller specs specs. They want you to test your views in view specs, not controller specs. Since the views don't render, assert_select
always fails.
But for people who (like me) want to test the occasional snippet of a view in their controller specs, they provide a render_views
method. You have to call it in your describe
or context
block, though, not inside the it
block.
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=?]", "/user/login" do
assert_select "input[name=?]", "username"
assert_select "input[name=?]", "password"
assert_select "input[type=?]", "submit"
end
end
end
Controller tests are for testing controllers.
assert_select
matches something that is in your view code.
It is a good idea keep your controllers separated from your views, and this includes tests done on controllers and on views. You should use assert_select
in your views test (the ones that are usually on spec/views
), not on your controller tests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With