我可以从装载它的真实应用程序运行Rails引擎的规格吗? [英] Can I run a Rails engine's specs from a real app which mounts it?

查看:124
本文介绍了我可以从装载它的真实应用程序运行Rails引擎的规格吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails引擎,旨在为我们的一个更大的项目提供一些模型和控制器。 Engine有一套相当不错的规格,在引擎的虚拟应用程序中使用一系列mock和一些全尺寸模型和控制器,以确保引擎正在做它应有的功能,并与更大的应用程序一起工作。



然而,即使所有测试都通过了,当我在更大的应用程序中更新引擎时,我经常发现破坏的行为。如果我的测试通过但行为被破坏,测试显然有些问题,但是什么?我嘲笑太多,还是不够?



为了让我更接近于搞清楚这一点,我希望能够从完整的内部运行引擎的测试应用。这似乎应该是可能的,但我不明白rspec足够彻底得到如何处理。



(这与这个问题,但不完全相同;我不是试图从一个命令运行所有规格,只是在完整的应用程序中运行引擎的规格环境。也似乎是相关的。实际上,我读过的每一个问题都标有 rspec rails-engines - 没有太多 - 而且它们都不是我所需要的,或未答复。)

解决方案

最简单的解决方案是在rspec命令中指定路径。如果您有目录结构

  / project 
/ engine
/ engine_2

然后你应该运行所有的规格

  cd project 
rspec spec / ../engine/spec ../engine_2/spec

但是如果你想在Continous Integration上运行规范,或者这看起来不太舒服,我通过定制的 rake spec 任务来改变模式方法来解决这个问题。 p>

lib / task / rspec.rake应该看起来像这样

  require rspec / core / rake_task

RSpec :: Core :: RakeTask.new(:spe​​c)

task:default => :spec
RSpec :: Core :: RakeTask.module_eval do
def pattern
extras = []
Rails.application.config.rspec_paths.each do | dir |
if File.directory?(dir)
extras<< File.join(dir,'spec','**','* _spec.rb').to_s
end
end
[@pattern] | extras
end
end

在引擎类中添加一个config的路径。 rspec_paths

  class Engine< :: Rails :: Engine 
#注册rspec的路径
config.rspec_paths<< self.root
end

不要忘记初始化config.rspec_paths中的某处基本项目。

如果你想添加工厂,那么你可以创建初始化程序,你可以在这里找到解决方案在stackoverflow上的解决方案。



不知道这个解决方案是否最好,但对我有用,我对此感到满意。祝你好运!


I have a Rails Engine meant to provide some models and controllers to a larger project of ours. There's a pretty decent set of specs for the Engine, using a bunch of mocks and some full-scale models and controllers within the engine's dummy app to make sure the Engine is doing what it's supposed to and working with the larger application.

However, even with all tests passing, I frequently find broken behavior when I update the engine in the larger application. If my tests are passing but the behavior is broken, clearly something's wrong with the tests, but what? Am I mocking too much, or not enough?

To get me closer to figuring this out, I'd like to be able to run the engine's tests from inside the full application. This seems like it should be possible, but I don't understand rspec thoroughly enough to get a handle on how.

(This is related to this question but not quite the same; I'm not trying to run all the specs from one command, just to run the engine's specs within the full app environment. This also seems to be related. Actually, I've read every question tagged with both and --there aren't many--and they're all either not what I need, or unanswered.)

解决方案

The simplest solution would be to specify the paths in rspec command. If you have directory structure

/project
/engine
/engine_2

Then you do and should run all the specs

cd project
rspec spec/ ../engine/spec ../engine_2/spec

But if you want to run specs on Continous Integration or just this doesn't seem to be comfortable I solved this problem with a customized rake spec task, changing the pattern method.

lib/task/rspec.rake should look like this

require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
RSpec::Core::RakeTask.module_eval do
  def pattern
    extras = []
    Rails.application.config.rspec_paths.each do |dir|
      if File.directory?( dir )
        extras << File.join( dir, 'spec', '**', '*_spec.rb' ).to_s
      end
    end
    [@pattern] | extras
  end
end

In engine class you add a path to config.rspec_paths

class Engine < ::Rails::Engine
  # Register path to rspec
  config.rspec_paths << self.root
end

And don't forget to initialize config.rspec_paths somewhere in a base project.

If you want to add factories then you can create initializer, you can find solution somewhere here on stackoverflow.

Not sure if this solution is the best but works for me and I am happy with that. Good luck!

这篇关于我可以从装载它的真实应用程序运行Rails引擎的规格吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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