简体   繁体   English

使用Rspec测试Rails 3.1可安装引擎

[英]Testing Rails 3.1 mountable engine with Rspec

I started making a Rails 3.1 engine, and I'm having a hard time testing it using rspec. 我开始制作Rails 3.1引擎,我很难用rspec测试它。

First of all, if I run rails g integration_test whatever it creates a regular integration test in tests/integration instead of spec/requests (the rspec-rails gem is installed and required as a development dependency in the gemspec file) 首先,如果我运行rails g integration_test whatever它会在测试/集成中创建常规集成测试而不是规范/请求(rspec-rails gem已安装并且需要作为gemspec文件中的开发依赖项)

Also, when I run a spec test I get an error saying the table corresponding to the model I'm testing has not been created. 此外,当我运行规范测试时,我得到一个错误,说明与我正在测试的模型相对应的表尚未创建。 I tried rake engine_name:install:migrations and running rake db:migrate from inside the dummy app, and I get a "table already exists" error. 我尝试了rake engine_name:install:migrations并从虚拟应用程序内部运行rake db:migrate ,我得到一个“表已存在”错误。

Everything just seems disconnected, I feel I'm missing something here to make the rspec gem work seamlessly as it usually does with full rails applications. 一切似乎都断开了,我觉得我在这里遗漏了一些东西,使rspec gem无缝地工作,就像通常使用完整的rails应用程序一样。

I followed all the changes from here http://rubyx.com/2011/03/01/start-your-engines and I can test the engine manually by launching the dummy app via the console as shown here http://railscasts.com/episodes/277-mountable-engines . 我从http://rubyx.com/2011/03/01/start-your-engines跟踪了所有更改,我可以通过控制台启动虚拟应用程序手动测试引擎,如http:// railscasts所示。 com / episodes / 277-mountable-engines

Is there a way to make rspec the default for testing a rails 3.1 engine? 有没有办法让rspec成为测试rails 3.1引擎的默认设置?

I am using RSpec with a Rails engine without issues. 我正在使用带有Rails引擎的RSpec而没有问题。

I created my plugin using the following switches: -T --full --dummy-path=spec/dummy. 我使用以下开关创建了我的插件:-T --full --dummy-path = spec / dummy。

  • -T excludes test/unit -T不包括测试/单位
  • --full indicates that the plugin is an engine --full表示插件是引擎
  • --dummy-path is simply so that we don't get a test directory (the default is test/dummy). --dummy-path只是为了让我们没有得到一个测试目录(默认是test / dummy)。

From there I used the spec_helper from the "start your engines" article: 从那里我使用了“启动引擎”文章中的spec_helper:

# Configure Rails Envinronment
ENV["RAILS_ENV"] = "test"
require File.expand_path("../dummy/config/environment.rb",  __FILE__)

require 'rspec/rails'

ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[File.join(ENGINE_RAILS_ROOT, "spec/support/**/*.rb")].each {|f| require f }

RSpec.configure do |config|
  config.use_transactional_fixtures = true
end

For the generators. 对于发电机。 I add a config.generators block to my engine.rb file like so: 我将config.generators块添加到我的engine.rb文件中,如下所示:

module MyEngine
  class Engine < Rails::Engine
    config.generators do |g|
      g.test_framework :rspec, :view_specs => false
    end
  end
end

With that, I'm able to get rspec tests when running a generator like the model generator. 有了这个,我可以在运行像模型生成器这样的生成器时获得rspec测试。

As for the DB, is your database.yml file set up correctly? 至于DB,你的database.yml文件设置正确吗? Did you load the test environment, eg rake db:test:clone or rake db:migrate RAILS_ENV=test ? 您是否加载了测试环境,例如rake db:test:clonerake db:migrate RAILS_ENV=test My guess is that RSpec can't see your tables because there isn't a test database set up. 我的猜测是RSpec无法查看您的表,因为没有设置测试数据库。

I was looking for the same answer and I found the combustion gem * which promise to setup a full environment for spec'ing your engine in a simpler way. 我正在寻找相同的答案,我找到了燃烧宝石 *,它承诺以更简单的方式设置一个完整的环境来指定你的引擎。 Just add 只需添加

gem.add_development_dependency 'combustion', '~> 0.3.1'

to your gemspec and run 到您的gemspec并运行

bundle exec combust

to reproduce a full rails app in your spec directory. 在spec目录中重现完整的rails应用程序。

*I haven't tried it yet... *我还没试过......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM