从Ruby on Rails教程生成测试文件

时间:2016-11-04 21:36:13

标签: ruby-on-rails ruby tdd

第5章练习“full_title”测试。我尝试了上一个文件生成示例中的模式...

$ rails generate integration_test site_layout

当我尝试

$ rails generate helpers_test application_helper,

我应该得到的路径是test/helpers/application_helper_test.rb,但我得到的错误是"Could not find generator 'helpers' "

以下是应该生成的类

require 'test_helper
class ApplicationHelperTest < ActionView::TestCase
  test 'full title helper' do
  ....
  ....
end
end

问题是我该怎样做才能在正确的位置生成正确的文件并获得正确的类。

我去了rails docs,我回顾了另一个问题Hartl Rail Tutorial: Chapter 5, exercise 3

2 个答案:

答案 0 :(得分:0)

我不确定你在哪里找到了对helpers_test生成器的引用,但它不是核心的Rails。

这可能取决于您拥有的Rails版本,但您应该可以这样做:

rails g helper application

这将生成以下2个文件(假设您使用的是Test :: Unit)

app/helpers/application_helper.rb
test/helpers/application_helper_test.rb

这肯定适用于Rails 4.x,但是对于我的Rails 5.x设置,这个生成器只创建了app/helpers/application_helper.rb,导致我怀疑该教程最初是为Rails 4创建的,并且没有针对Rails 5正确更新

您只需调用以下内容即可找到您拥有的生成器:

rails g --help

有关特定生成器的更多信息,请通过以下方式进行扩展:

rails g helper --help

顺便说一下,没有什么可以阻止你自己在正确的位置自己创建文件。

答案 1 :(得分:0)

  

练习片段

     

...通过编写full_title帮助器的直接测试,其中涉及到   创建文件以测试应用程序帮助程序,然后填写   代码用FILL_IN表示......

使用命令touch创建文件,然后将目录和文件名添加到bash /命令行中(记得cd进入rails应用程序):

$ touch test/helpers/application_helper_test.rb

然后手动打开并添加以下内容:

class ApplicationHelperTest < ActionView::TestCase
  test "full title helper" do
    assert_equal full_title,         FILL_IN
    assert_equal full_title("Help"), FILL_IN
  end
end

显然,FILL_IN说你要编写一些代码:)