The instruction is this:
Following the template in Listing 11.68, write a test of the image uploader in Section 11.4. As preparation, you should add an image to the fixtures directory (using, e.g, cp app/assets/images/rails.png test/fixtures/). (If you’re using Git, I also recommend updating your .gitignore file as shown in Listing 11.69.) To avoid a confusing error, you will also need to configure CarrierWave to skip image resizing in tests by creating an initializer file as shown in Listing 11.70. The additional assertions in Listing 11.68 check both for a file upload field on the Home page and for a valid image attribute on the micropost resulting from valid submission. Note the use of the special fixture_file_upload method for uploading files as fixtures inside tests.22 Hint: To check for a valid picture attribute, use the assigns method mentioned in Section 10.1.4 to access the micropost in the create action after valid submission.
Here's my code in test/integration/micrposts_interface_test.rb
test "micropost interface" do
log_in_as(@user)
get root_path
assert_select 'div.pagination'
assert_select 'input[type=FILL_IN]'
# Invalid submission
assert_no_difference 'Micropost.count' do
post microposts_path, micropost: { content: "" }
end
assert_select 'div#error_explanation'
# Valid submission
content = "This micropost really ties the room together"
picture = fixture_file_upload('test/fixtures/rails.png', 'image/png')
assert_difference 'Micropost.count', 1 do
post microposts_path, micropost: { content: content, picture: FILL_IN }
end
assert FILL_IN.picture?
follow_redirect!
assert_match content, response.body
# Delete a post.
assert_select 'a', text: 'delete'
first_micropost = @user.microposts.paginate(page: 1).first
assert_difference 'Micropost.count', -1 do
delete micropost_path(first_micropost)
end
# Visit a different user.
get user_path(users(:archer))
assert_select 'a', text: 'delete', count: 0
end
In assert_select 'input[type=FILL_IN]', my fill-in is type=file
In post microposts_path, micropost: { content: content, picture: FILL_IN }, my fill-in is picture: true
In assert FILL_IN.picture? is @user.microposts.picture?
The error I get is: NoMethodError: NoMethodError: undefined method `picture?' for #
So I think my first two FILL_INs are correct. The problem comes from the third fill-in. I wrote @user.microposts because I thought to test for the presence of a picture?, it'll have to be on the microposts of the user. But the error was no method for picture?
I tried also @user.micropost.picture?, and the error was NoMethodError: NoMethodError: undefined method `micropost' for #
I thought my line of reasoning was correct, but apparently not. Help! I'm a complete newb.