如何使用Cucumber和Rspec测试Rails 3引擎?
如果这个问题有点主观,我很抱歉…我正在试图找出用黄瓜和Rspec测试Rails 3引擎的最佳方法。 为了测试发动机,需要使用导轨3 app。 这是我目前正在做的事情:
-
通过运行:
rails new /myengine/rails_app
将rails测试应用程序添加到gem(myengine)的根目录 -
像在普通的Rails应用程序中一样,将Cucumber添加到
/myengine/rails_app/features
-
在
/myengine/rails_app/Gemfile
需要Rails Engine Gem(使用:path=>"/myengine"
) -
将spec添加到gem的根目录:
/myengine/spec
-
在
/myengine/spec/fixtures
包含/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)
你看到这样设置有什么问题吗? 测试运行良好,但我有点犹豫是否将function放在测试轨应用程序中。 我最初尝试将这些function放在gem的根目录中,并在features/support
创建了测试rails应用程序,但由于某些原因,我运行测试时我的引擎无法初始化,即使我可以看到应用程序加载其他所有内容时瓜跑了。
如果有人正在使用Rails Engines并使用cuc和rspec进行测试,我很想听听你的设置。
* *更新
自从我写这个问题以后,我改变了我的设置。 我决定摆脱引擎根目录下的spec目录。 现在我只需创建一个名为“test_app”的rails应用程序,并在该应用程序中设置cuc和rspec,就像我通常在rails应用程序中所做的那样。 然后我像上面步骤#3中那样包含gem。 由于引擎是一个子应用程序,我想它最好测试它就像一个普通的rails应用程序。 如果有人有不同的设置,我仍然有兴趣听听。
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 g cucumber:install
到项目中然后搞乱它直到它工作。 我之前已经做过一次,所以我知道这是可能的,但我现在找不到代码。
让我知道你怎么去。
我将解释如何使用以下gem作为示例: https : //github.com/skozlov/netzke-core
测试应用程序 。 它位于netzke-core/test/rails_app
。 这个应用程序可以独立运行,所以我也可以使用它进行手动测试或者如果我喜欢的话可以使用新function。
为了让测试应用程序加载gem本身,我在application.rb
有以下内容:
$:.unshift File.expand_path('../../../../lib', __FILE__) require 'netzke-core'
黄瓜的特点 。 它们属于netzke-core/features
。 在env.rb
我有:
require File.expand_path(File.dirname(__FILE__) + '/../../test/rails_app/config/environment')
…将在执行function之前加载测试应用程序。
规格 。 这些是netzke-core/spec
。 在spec_helper.rb
我有以下内容:
require File.expand_path("../../test/rails_app/config/environment", __FILE__)
…将在运行规范之前加载测试应用程序。
运行测试 。 这个设置让我从gem的根目录运行测试:
cucumber features
和
rspec spec
工厂女孩 。 不是这个特定的gem,但我通常使用factory_girl而不是fixtures(例如,参见https://github.com/skozlov/netzke-basepack中的类似设置)。
派对有点晚了,但这是我的策略:
-
在3.2中生成rails插件:
rails plugin new blog --mountable --full
这创建了
test/dummy
,包含虚拟rails应用程序 -
将规格添加到
spec
-
将虚拟文件夹移动到
spec
(并可选择删除其他testfiles) -
适应
specs/spec_helper.rb
,使其包括在内require File.expand_path("../.../config/environment", __FILE__)
代替
require File.expand_path("../dummy/config/environment", __FILE__)
-
执行
rails g cucumber:install
。 它将生成features
文件夹ao -
加
ENV["RAILS_ROOT"] ||= File.expand_path(File.dirname(__FILE__) + '/../../spec/dummy')
之前
require 'cucumber/rails'
在
features/support/env.rb
现在你在项目的根目录中有features
和spec
,而虚拟轨道应用程序整齐地隐藏在spec/dummy