简体   繁体   English

如何在Rails控制台中重现Rails单元测试(有关路线的错误)

[英]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. 我是一位经验丰富的程序员,但是是Ruby和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. 我想尝试为现有项目构建新测试,所以我认为一个好的开始方法是在Rails控制台中逐行复制现有测试。 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: 因此,我尝试按如下所示调整$ LOAD_PATH,这似乎可行:

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? 如何解决缺少的“路线”,以便可以从Rails控制台运行测试? Or is my goal inadvisable to begin with? 还是我的目标不明智?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM