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

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

问题描述

我有一个 Rails 引擎,旨在为我们更大的项目提供一些模型和控制器.引擎有一套相当不错的规范,在引擎的虚拟应用程序中使用了一堆模拟和一些全尺寸模型和控制器,以确保引擎正在做它应该做的事情并与更大的应用程序一起工作.

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?

为了让我更接近于解决这个问题,我希望能够从完整的应用程序内部运行引擎的测试.这似乎应该是可能的,但我对 rspec 的理解不够透彻,无法掌握如何操作.

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 rspec and rails-engines--there aren't many--and they're all either not what I need, or unanswered.)

推荐答案

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

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

但是如果你想在 Continous Integration 上运行规范,或者只是这似乎不太舒服,我通过一个定制的 rake 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 应该是这样的

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

在引擎类中添加一个路径到 config.rspec_paths

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

并且不要忘记在基础项目中的某处初始化 config.rspec_paths.

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

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

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天全站免登陆