0

I'm an experienced programmer, but brand new to Ruby and Rails. I want to try building new tests for an existing project, so I thought a good way to start would be to reproduce existing tests line by line in the Rails console. However, I haven't been able to make it work.

As this question suggests, I went to the main directory of the project and did this:

$ rails console -e test
Loading test environment (Rails 5.2.0)
irb(main):001:0> require './test/test_helper'
[Coveralls] Set up the SimpleCov formatter.
[Coveralls] Using SimpleCov's 'rails' settings.
   (1.0ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
=> true

So far so good. But I still couldn't load the actual test modules:

irb(main):002:0> require './test/controllers/users_controller_test'
Traceback (most recent call last):
        2: from (irb):2
        1: from test/controllers/users_controller_test.rb:1:in `<top (required)>'
LoadError (cannot load such file -- test_helper)

So then I tried adjusting the $LOAD_PATH as follows, which seems to work:

irb(main):003:0> $LOAD_PATH.unshift '/{{absolute path to project}}/test'
{{irb now prints the new $LOAD_PATH which is really long}}
irb(main):004:0> require './test/controllers/users_controller_test'                         
=> true

But when I try to run the actual tests, it's still not working. It looks like some kind of unloaded "routes":

irb(main):008:0> my_test = UsersControllerTest.new("My Test")
=> #<UsersControllerTest:0x000055c90aadd148 @NAME="My Test", @failures=[], @assertions=0>
irb(main):009:0> my_test.test_routes
Traceback (most recent call last):
        2: from (irb):9
        1: from test/controllers/users_controller_test.rb:11:in `test_routes'
NoMethodError (undefined method `recognize_path' for nil:NilClass)
irb(main):010:0> my_test.test_new_view
Traceback (most recent call last):
        2: from (irb):10
        1: from test/controllers/users_controller_test.rb:191:in `test_new_view'
RuntimeError (@routes is nil: make sure you set it in your test's setup method.)

Is there an easier way to run tests from the console? How can I resolve the missing "routes" so I can run the tests from the rails console? Or is my goal inadvisable to begin with?

krubo
  • 5,969
  • 4
  • 37
  • 46
  • 2
    I would say it's a non standard approach. If I were to do this, I would write an empty test and put only `binding.pry` inside ([pry](https://github.com/pry/pry) is a popular debugger). By running the single test and hitting the breakpoint you would be sure that everything is initialized properly and you could start tinkering in the console. – Marcin Kołodziej Nov 15 '18 at 01:40
  • @MarcinKołodziej that sounds great! So I did `gem install pry` and confirmed that `require 'pry'` works in `irb`. Then I added `require 'pry'` at the top of the test .rb file and `binding.pry` in the middle and ran the file with `rails test`. But I get a many-lines error ending with "cannot load such file -- pry (LoadError)" Any idea why? – krubo Nov 15 '18 at 02:11
  • Try adding `gem "pry"` to your `Gemfile`, running `bundle install` and `bundle exec rails test`. – Marcin Kołodziej Nov 15 '18 at 02:13
  • 1
    @MarcinKołodziej it works! I did `gem install pry`, added `gem "pry"` to the project's Gemfile, added `require 'pry'` at the top of the test .rb file and `binding.pry` in the middle. Still can't run most of the tests for various reasons, but `pry` is a good starting point for now. – krubo Nov 15 '18 at 02:23

0 Answers0