In two previous posts, I wrote about my frustrations with methods in Rails tests that just seem to float in space and appear out of nowhere, and that the Hartl tutorial mixes RSpec and Capybara, and that it would be nice to know which is which.
In the last post, I wrote that sometimes when you try to find which class contains a method you get “Object”, which is not very helpful.
1 2 |
puts "Here is method of sign_in: #{self.method(:sign_in).owner} " puts "Here is method of visit: #{self.method(:visit).owner} " |
Here is the output:
1 2 |
Here is method of sign_in: Object Here is method of visit: Capybara::DSL |
The “sign_in” method is from one of the tests in the Hartl tutorial. I was explaining this to someone about an hour ago, and I realized why self.method says that “sign_in” is part of the “Object” class: Because it is not defined in a class. In the application, Hartl defines it in a module, but in the tests it is simply defined in a file that contains neither a class or a module.
In the root of the Rails tutorial app, you can run a grep command to find that method:
1 |
grep -rn 'def sign_in' * |
This will give you the following result
1 2 |
app/helpers/sessions_helper.rb:3: def sign_in(user) spec/support/utilities.rb:3:def sign_in(user) |
So here is the file spec/support/utilities.rb:
1 2 3 4 5 6 7 8 9 10 11 |
include ApplicationHelper def sign_in(user) visit signin_path fill_in "Email", with: user.email fill_in "Password", with: user.password click_button "Sign in" # puts "Here is method of click_button: #{self.method(:click_button).owner} - " # Sign in when not using Capybara as well. cookies[:remember_token] = user.remember_token end |
As I stated, in app/helpers/sessions_helper.rb the method is in a module called SessionsHelper