如何使用 Cucumber & 测试 Rails 3 引擎规格? [英] How to test Rails 3 Engines with Cucumber & Rspec?

查看:46
本文介绍了如何使用 Cucumber & 测试 Rails 3 引擎规格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 通过运行以下命令将 rails 测试应用程序添加到 gem (myengine) 的根目录:rails new/myengine/rails_app

将 Cucumber 添加到 /myengine/rails_app/features,就像在普通 Rails 应用中一样

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

/myengine/rails_app/Gemfile

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

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

/myengine/spec/fixtures 中包含夹具,然后将以下内容添加到 cuc env.rb 中:

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)

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

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.

如果有人使用 Rails 引擎并使用 cuc 和 rspec 进行测试,我很想听听您的设置.

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

**更新
自从我写了这个问题后,我稍微改变了我的设置.我决定去掉引擎根目录下的 spec 目录.现在我只是创建一个名为test_app"的 rails 应用程序并在该应用程序中设置 cuc 和 rspec,就像我通常在 rails 应用程序中所做的那样.然后我像我在上面的第 3 步中所做的那样包含 gem.由于引擎是一个子应用程序,我想最好像测试普通的 rails 应用程序一样测试它.我仍然很想知道是否有人有不同的设置.

**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.

推荐答案

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

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

然后安装边轨:

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

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

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

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

简单地:

rails plugin new blog --mountable --full

mountable 选项使应用程序可挂载,而 full 选项使其成为已内置测试的引擎.为了测试引擎,此生成器在 test 中生成一个名为 dummy 的文件夹,其中包含一个小型 Rails 应用程序.您可以在 test/test_helper.rb 中看到它是如何加载的.

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.

然后由您来处理数据以执行其工作所需的操作.我建议将标准 rails gucumber:install 中的黄瓜文件复制到项目中,然后对其进行处理,直到它起作用为止.我以前做过一次,所以我知道这是可能的,但我现在找不到代码.

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.

让我知道你的去向.

这篇关于如何使用 Cucumber & 测试 Rails 3 引擎规格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆