如何使用 Cucumber 测试 Rails 3 引擎规格?
如果这个问题有点主观,我深表歉意...我正在尝试找出用 Cucumber 和 Rails 3 引擎测试的最佳方法。 R规格。为了测试引擎,需要 Rails 3 应用程序。这是我当前正在做的事情:
通过运行以下命令将 Rails 测试应用程序添加到 gem (myengine) 的根目录:
rails new /myengine/rails_app
将 Cucumber 添加到
/myengine/rails_app/features
,就像在正常情况下一样Rails 应用程序需要 Rails Engine Gem(使用
:path=>"/ myengine"
) 位于/myengine/rails_app/Gemfile
将spec添加到gem的根目录:
/myengine/spec
将灯具包含在
/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:
Add a rails test app to the root of the gem (myengine) by running:
rails new /myengine/rails_app
Add Cucumber to
/myengine/rails_app/features
as you would in a normal Rails appRequire the Rails Engine Gem (using
:path=>"/myengine"
) in/myengine/rails_app/Gemfile
Add spec to the root directory of the gem:
/myengine/spec
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Rails 3.1(将)为引擎生成一个非常好的脚手架。我建议使用 RVM 创建一个名为 Edge 的新 gemset 并切换到它:
然后安装边缘导轨:
从那里,您可以按照 Piotr Sarnacki 的可安装应用程序教程,替换如下调用:
简单地:
可安装选项使应用程序可安装,而完整选项使其成为已构建测试的引擎 -在。为了测试引擎,该生成器在
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:
Then install edge rails:
From there, you can follow Piotr Sarnacki's mountable app tutorial, replacing calls such as:
With simply:
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
calleddummy
which contains a small Rails application. You can see how this is loaded intest/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.
我将使用以下 gem 作为示例来解释我是如何做到的: https://github.com/skozlov/ netzke-core
测试应用程序。它位于
netzke-core/test/rails_app
中。这个应用程序可以独立运行,因此我也可以使用它进行手动测试或尝试新功能(如果我愿意)。为了让测试应用程序加载 gem 本身,我在
application.rb
中添加了以下内容:Cucumber 功能。它们位于
netzke-core/features
中。在 env.rb 中,我有:...它将在执行功能之前加载测试应用程序。
规格。这些位于 netzke-core/spec 中。在
spec_helper.rb
中,我有以下内容:...它将在运行规范之前加载测试应用程序。
运行测试。 Factory Girl的根目录运行测试
这个设置让我可以从 gem:和
。不适用于这个特定的 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
:Cucumber features. They are in
netzke-core/features
. Inenv.rb
I have:... which will load the testing application before executing the features.
Specs. These are in
netzke-core/spec
. Inspec_helper.rb
I have the following:... 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:
and
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).
虽然有点晚了,但这是我的策略:
在 3.2 中生成 Rails 插件:
这会创建
test/dummy
,包含虚拟rails应用程序将规格添加到
spec
将虚拟文件夹移动到
spec
(并且可以选择删除其他测试文件)Adapt
specs/spec_helper.rb
所以它包括而不是
执行
rails g cucumber:install
。它将生成features
文件夹 aoAdd
之前
在
features/support/env.rb
现在你有
features
和spec
位于项目的根目录中,而虚拟 Rails 应用程序则整齐地隐藏在spec/dummy
下A bit late to the party, but here is my strategy:
Generating the rails plugin in 3.2:
This creates
test/dummy
, containing the dummy rails appAdd the specs to
spec
Move the dummy folder to
spec
(and optionally get rid of the other testfiles)Adapt
specs/spec_helper.rb
so it includesinstead of
Execute
rails g cucumber:install
. It will generatefeatures
folder a.o.Add
before
in
features/support/env.rb
Now you have
features
andspec
in the root of you project, while the dummy rails app is neatly tucked away underspec/dummy