How to reproduce Rails unit tests in the Rails console (error about routes)
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?
ruby-on-rails testing
add a comment |
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?
ruby-on-rails testing
2
I would say it's a non standard approach. If I were to do this, I would write an empty test and put onlybinding.pry
inside (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 1:40
@MarcinKołodziej that sounds great! So I didgem install pry
and confirmed thatrequire 'pry'
works inirb
. Then I addedrequire 'pry'
at the top of the test .rb file andbinding.pry
in the middle and ran the file withrails test
. But I get a many-lines error ending with "cannot load such file -- pry (LoadError)" Any idea why?
– krubo
Nov 15 '18 at 2:11
Try addinggem "pry"
to yourGemfile
, runningbundle install
andbundle exec rails test
.
– Marcin Kołodziej
Nov 15 '18 at 2:13
1
@MarcinKołodziej it works! I didgem install pry
, addedgem "pry"
to the project's Gemfile, addedrequire 'pry'
at the top of the test .rb file andbinding.pry
in the middle. Still can't run most of the tests for various reasons, butpry
is a good starting point for now.
– krubo
Nov 15 '18 at 2:23
add a comment |
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?
ruby-on-rails testing
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?
ruby-on-rails testing
ruby-on-rails testing
asked Nov 15 '18 at 1:35
krubokrubo
1,74611626
1,74611626
2
I would say it's a non standard approach. If I were to do this, I would write an empty test and put onlybinding.pry
inside (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 1:40
@MarcinKołodziej that sounds great! So I didgem install pry
and confirmed thatrequire 'pry'
works inirb
. Then I addedrequire 'pry'
at the top of the test .rb file andbinding.pry
in the middle and ran the file withrails test
. But I get a many-lines error ending with "cannot load such file -- pry (LoadError)" Any idea why?
– krubo
Nov 15 '18 at 2:11
Try addinggem "pry"
to yourGemfile
, runningbundle install
andbundle exec rails test
.
– Marcin Kołodziej
Nov 15 '18 at 2:13
1
@MarcinKołodziej it works! I didgem install pry
, addedgem "pry"
to the project's Gemfile, addedrequire 'pry'
at the top of the test .rb file andbinding.pry
in the middle. Still can't run most of the tests for various reasons, butpry
is a good starting point for now.
– krubo
Nov 15 '18 at 2:23
add a comment |
2
I would say it's a non standard approach. If I were to do this, I would write an empty test and put onlybinding.pry
inside (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 1:40
@MarcinKołodziej that sounds great! So I didgem install pry
and confirmed thatrequire 'pry'
works inirb
. Then I addedrequire 'pry'
at the top of the test .rb file andbinding.pry
in the middle and ran the file withrails test
. But I get a many-lines error ending with "cannot load such file -- pry (LoadError)" Any idea why?
– krubo
Nov 15 '18 at 2:11
Try addinggem "pry"
to yourGemfile
, runningbundle install
andbundle exec rails test
.
– Marcin Kołodziej
Nov 15 '18 at 2:13
1
@MarcinKołodziej it works! I didgem install pry
, addedgem "pry"
to the project's Gemfile, addedrequire 'pry'
at the top of the test .rb file andbinding.pry
in the middle. Still can't run most of the tests for various reasons, butpry
is a good starting point for now.
– krubo
Nov 15 '18 at 2:23
2
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 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 1:40
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 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 1: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 2:11
@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 2:11
Try adding
gem "pry"
to your Gemfile
, running bundle install
and bundle exec rails test
.– Marcin Kołodziej
Nov 15 '18 at 2:13
Try adding
gem "pry"
to your Gemfile
, running bundle install
and bundle exec rails test
.– Marcin Kołodziej
Nov 15 '18 at 2:13
1
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 2:23
@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 2:23
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311237%2fhow-to-reproduce-rails-unit-tests-in-the-rails-console-error-about-routes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311237%2fhow-to-reproduce-rails-unit-tests-in-the-rails-console-error-about-routes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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 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 1:40
@MarcinKołodziej that sounds great! So I did
gem install pry
and confirmed thatrequire 'pry'
works inirb
. Then I addedrequire 'pry'
at the top of the test .rb file andbinding.pry
in the middle and ran the file withrails test
. But I get a many-lines error ending with "cannot load such file -- pry (LoadError)" Any idea why?– krubo
Nov 15 '18 at 2:11
Try adding
gem "pry"
to yourGemfile
, runningbundle install
andbundle exec rails test
.– Marcin Kołodziej
Nov 15 '18 at 2:13
1
@MarcinKołodziej it works! I did
gem install pry
, addedgem "pry"
to the project's Gemfile, addedrequire 'pry'
at the top of the test .rb file andbinding.pry
in the middle. Still can't run most of the tests for various reasons, butpry
is a good starting point for now.– krubo
Nov 15 '18 at 2:23