I’m running a test that I’ve created in rails using a simple assert_select that should populate 5 divs based on my fixtures. But every time it’s failing saying 0 elements were found, when 5 were expected. Is it possible to see the output that the test is basing this on?
Below is the specific test that’s giving me trouble:
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
assert_select '.contentBox', minimum: 5
end
end
and the error:
Finished tests in 0.403847s, 2.4762 tests/s, 4.9524 assertions/s. 1) Failure: test_should_get_index(PostsControllerTest) [/var/www/site/test/functional/posts_controller_test.rb:7]: Expected at least 5 elements matching ".contentBox", found 0. 1 tests, 2 assertions, 1 failures, 0 errors, 0 skips rake aborted!
It should be noted that when manually adding everything from rails console (e.g. Post.new, etc) everything works fine and I can manually check the source and see that there are the appropriate number of items. So it leads me to believe that I’ve done something wrong with the test.
You should be able to use
puts @response.bodyto print the body of the page that is returned if you want to inspect it. You could also consider using Capybara which if you did you could useput page.bodyWhat you will have to add to the test is creating 5 posts. So doing something like:
Update for question in comments:
If you have in your controller:
@posts = Post.allThen in your view you should have something like: