如何使用 Cucumber 测试 Rails 3 引擎规格?

发布于 2024-10-06 15:50:55 字数 1344 浏览 1 评论 0原文

如果这个问题有点主观,我深表歉意...我正在尝试找出用 Cucumber 和 Rails 3 引擎测试的最佳方法。 R规格。为了测试引擎,需要 Rails 3 应用程序。这是我当前正在做的事情:

  1. 通过运行以下命令将 Rails 测试应用程序添加到 gem (myengine) 的根目录: rails new /myengine/rails_app

  2. 将 Cucumber 添加到 /myengine/rails_app/features ,就像在正常情况下一样Rails 应用程序

  3. 需要 Rails Engine Gem(使用 :path=>"/ myengine") 位于 /myengine/rails_app/Gemfile

  4. 将spec添加到gem的根目录: /myengine/spec

  5. 将灯具包含在 /myengine/spec/fixtures 中,并将以下内容添加到我的 cuc env.rb 中:

env.rb:

Fixtures.reset_cache
fixtures_folder = File.join(Rails.root, 'spec', 'fixtures')  
fixtures = Dir[File.join(fixtures_folder, '*.yml')].map {|f| File.basename(f, '.yml') }  
Fixtures.create_fixtures(fixtures_folder, fixtures)

您发现这样设置有任何问题吗?测试运行良好,但我有点犹豫是否将这些功能放入测试轨道应用程序中。我最初尝试将这些功能放在 gem 的根目录中,并在 features/support 中创建了测试 Rails 应用程序,但由于某种原因,当我运行测试时,我的引擎不会初始化,即使我可以当 cuc 运行时,看到应用程序加载其他所有内容。

如果有人正在使用 Rails Engines 并使用 cuc 和 rspec 进行测试,我很想听听您的设置。

**更新
自从我写了这个问题以来,我改变了一些设置。我决定删除引擎根目录下的spec目录。现在,我只需创建一个名为“test_app”的 Rails 应用程序,并在该应用程序中设置 cuc 和 rspec,就像我通常在 Rails 应用程序中所做的那样。然后我像上面第 3 步中那样添加 gem。由于引擎是一个子应用程序,我想最好像测试普通的 Rails 应用程序一样测试它。我仍然有兴趣听听是否有人有不同的设置。

I apologize if this question is slightly subjective... I am trying to figure out the best way to test Rails 3 Engines with Cucumber & Rspec. In order to test the engine a rails 3 app is necessary. Here is what I am currently doing:

  1. Add a rails test app to the root of the gem (myengine) by running: rails new /myengine/rails_app

  2. Add Cucumber to /myengine/rails_app/features as you would in a normal Rails app

  3. Require the Rails Engine Gem (using :path=>"/myengine") in /myengine/rails_app/Gemfile

  4. Add spec to the root directory of the gem: /myengine/spec

  5. Include the fixtures in /myengine/spec/fixtures and I add the following to my cuc env.rb:

env.rb:

Fixtures.reset_cache
fixtures_folder = File.join(Rails.root, 'spec', 'fixtures')  
fixtures = Dir[File.join(fixtures_folder, '*.yml')].map {|f| File.basename(f, '.yml') }  
Fixtures.create_fixtures(fixtures_folder, fixtures)

Do you see any problems with setting it up like this? The tests run fine, but I am a bit hesitant to put the features inside the test rails app. I originally tried putting the features in the root of the gem and I created the test rails app inside features/support, but for some reason my engine would not initialize when I ran the tests, even though I could see the app loading everything else when cuc ran.

If anyone is working with Rails Engines and is using cuc and rspec for testing, I would be interested to hear your setup.

**UPDATE
I changed my setup a bit since I wrote this question. I decided to get rid of the spec directory under the root of the engine. Now I just create a rails app named "test_app" and setup cuc and rspec inside that app like I would normally do in a rails app. Then I include the gem like I did in step #3 above. Since the engine is a sub-app, I guess its just best to test it like it was a normal rails app. I am still interested in hearing if anyone has a different setup.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缱倦旧时光 2024-10-13 15:50:55

Rails 3.1(将)为引擎生成一个非常好的脚手架。我建议使用 RVM 创建一个名为 Edge 的新 gemset 并切换到它:

rvm gemset create edge
rvm use @edge

然后安装边缘导轨:

git clone git://github.com/rails/rails.git
cd rails
rake install

从那里,您可以按照 Piotr Sarnacki 的可安装应用程序教程,替换如下调用:

bundle exec ./bin/rails plugin new ../blog --edge --mountable

简单地:

rails plugin new blog --mountable --full

可安装选项使应用程序可安装,而完整选项使其成为已构建测试的引擎 -在。为了测试引擎,该生成器在 test 中生成一个名为 dummy 的文件夹,其中包含一个小型 Rails 应用程序。您可以在 test/test_helper.rb 中看到它是如何加载的。

然后由您来处理数据以完成其工作所需的操作。我建议将标准 rails g cucumber:install 中的黄瓜文件复制到项目中,然后对其进行修改,直到它正常工作。我以前做过一次,所以我知道这是可能的,但我现在找不到代码。

让我知道你进展如何。

Rails 3.1 (will) generate a pretty good scaffold for engines. I'd recommend using RVM to create a new gemset called edge and switch to it:

rvm gemset create edge
rvm use @edge

Then install edge rails:

git clone git://github.com/rails/rails.git
cd rails
rake install

From there, you can follow Piotr Sarnacki's mountable app tutorial, replacing calls such as:

bundle exec ./bin/rails plugin new ../blog --edge --mountable

With simply:

rails plugin new blog --mountable --full

The mountable option makes the application mountable, whilst the full option makes it an engine with tests already built-in. To test the engine, this generator generates a folder in test called dummy which contains a small Rails application. You can see how this is loaded in test/test_helper.rb.

Then it's up to you to massage the data to do what it needs to in order to work. I would recommend copying over the cucumber files from a standard rails g cucumber:install into the project and then messing about with it until it works. I've done this once before so I know it's possible, but I cannot find the code right now.

Let me know how you go.

咋地 2024-10-13 15:50:55

我将使用以下 gem 作为示例来解释我是如何做到的: https://github.com/skozlov/ netzke-core

测试应用程序。它位于 netzke-core/test/rails_app 中。这个应用程序可以独立运行,因此我也可以使用它进行手动测试或尝试新功能(如果我愿意)。

为了让测试应用程序加载 gem 本身,我在 application.rb 中添加了以下内容:

$:.unshift File.expand_path('../../../../lib', __FILE__)
require 'netzke-core'

Cucumber 功能。它们位于 netzke-core/features 中。在 env.rb 中,我有:

require File.expand_path(File.dirname(__FILE__) + '/../../test/rails_app/config/environment')

...它将在执行功能之前加载测试应用程序。

规格。这些位于 netzke-core/spec 中。在 spec_helper.rb 中,我有以下内容:

require File.expand_path("../../test/rails_app/config/environment", __FILE__)

...它将在运行规范之前加载测试应用程序。

运行测试Factory Girl的根目录运行测试

cucumber features

这个设置让我可以从 gem:和

rspec spec

。不适用于这个特定的 gem,但我通常使用factory_girl 而不是固定装置(例如,参见 https://github.com/skozlov/netzke-basepack)。

I'll explain how I did it using as example the following gem: https://github.com/skozlov/netzke-core

The testing application. It is in netzke-core/test/rails_app. This app can be run independently, so I can also use it for manual testing or for playing around with new features if I like.

In order for the testing app to load the gem itself, I have the following in application.rb:

$:.unshift File.expand_path('../../../../lib', __FILE__)
require 'netzke-core'

Cucumber features. They are in netzke-core/features. In env.rb I have:

require File.expand_path(File.dirname(__FILE__) + '/../../test/rails_app/config/environment')

... which will load the testing application before executing the features.

Specs. These are in netzke-core/spec. In spec_helper.rb I have the following:

require File.expand_path("../../test/rails_app/config/environment", __FILE__)

... which will load the testing application before running the specs.

Running tests. This setup lets me run the tests from the root of the gem:

cucumber features

and

rspec spec

Factory Girl. Not for this particular gem, but I'm normally using factory_girl instead of fixtures (see, for example, a similar setup in https://github.com/skozlov/netzke-basepack).

↘人皮目录ツ 2024-10-13 15:50:55

虽然有点晚了,但这是我的策略:

  1. 在 3.2 中生成 Rails 插件:

    rails插件新博客--mountable --full
    

    这会创建test/dummy,包含虚拟rails应用程序

  2. 将规格添加到 spec

  3. 将虚拟文件夹移动到 spec(并且可以选择删除其他测试文件)

  4. Adapt specs/spec_helper.rb所以它包括

    require File.expand_path("../.../config/environment", __FILE__)
    

    而不是

    require File.expand_path("../dummy/config/environment", __FILE__)
    
  5. 执行rails g cucumber:install。它将生成 features 文件夹 ao

  6. Add

    ENV["RAILS_ROOT"] ||= File.expand_path(File.dirname(__FILE__) + '/../../spec/dummy')
    

    之前

    需要 'cucumber/rails'
    

    features/support/env.rb

现在你有featuresspec 位于项目的根目录中,而虚拟 Rails 应用程序则整齐地隐藏在 spec/dummy

A bit late to the party, but here is my strategy:

  1. Generating the rails plugin in 3.2:

    rails plugin new blog --mountable --full
    

    This creates test/dummy, containing the dummy rails app

  2. Add the specs to spec

  3. Move the dummy folder to spec (and optionally get rid of the other testfiles)

  4. Adapt specs/spec_helper.rb so it includes

    require File.expand_path("../.../config/environment", __FILE__)
    

    instead of

    require File.expand_path("../dummy/config/environment", __FILE__)
    
  5. Execute rails g cucumber:install. It will generate features folder a.o.

  6. Add

    ENV["RAILS_ROOT"] ||= File.expand_path(File.dirname(__FILE__) + '/../../spec/dummy')
    

    before

    require 'cucumber/rails'
    

    in features/support/env.rb

Now you have features and spec in the root of you project, while the dummy rails app is neatly tucked away under spec/dummy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文