ruby-on-rails - 编写 gem 时如何测试虚拟 Rails 应用程序?

标签 ruby-on-rails ruby testing rubygems

我正在编写一个与 Rails 集成的 gem,我希望能够在我的 gem 的测试套件中使用 rspec 测试一个虚拟应用程序。

当我测试我的虚拟 rails 应用程序是否通过加载/几个模块时,问题出现了 rspec 规范/integration/rails/load_path_spec.rb

到目前为止,这是我所拥有的:

# spec/support/rails_app/config/environment.rb

# Load the Rails application.
require File.expand_path('../application', __FILE__)

# Load the gem
require 'skinny_controllers'

# Initialize the Rails application.
Rails.application.initialize!

我的测试是这样的:

require 'rails_helper'

describe 'definitions of operations and policies' do
  it 'loads operations' do
    is_defined = defined? EventOperations
    expect(is_defined).to be_truthy
  end

  it 'loads policies' do
    is_defined = defined? EventPolicy
    expect(is_defined).to be_truthy
  end
end

rails_helper 看起来像这样:

require 'rails/all'

require 'factory_girl'
require 'factory_girl_rails'
require 'rspec/rails'

require 'support/rails_app/config/environment'

ActiveRecord::Migration.maintain_test_schema!

# set up db
# be sure to update the schema if required by doing
# - cd spec/rails_app
# - rake db:migrate
ActiveRecord::Schema.verbose = false
load "support/rails_app/db/schema.rb" # use db agnostic schema by default


require 'support/rails_app/factory_girl'
# require 'support/rails_app/factories'
require 'spec_helper'

spec_helper 看起来像这样:

require 'rubygems'
require 'bundler/setup'

require 'pry-byebug' # binding.pry to debug!
require 'awesome_print'

# Coverage
ENV['CODECLIMATE_REPO_TOKEN'] = ''
require 'codeclimate-test-reporter'
CodeClimate::TestReporter.start if ENV['TRAVIS']


# This Gem
require 'skinny_controllers'

$LOAD_PATH.unshift Dir[File.dirname(__FILE__) + '/support/operations'].first
$LOAD_PATH.unshift Dir[File.dirname(__FILE__) + '/support/policies'].first
Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each do |file|
  # skip the dummy app
  next if file.include?('support/rails_app')

  require file
end

# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# Require this file using `require "spec_helper"` to ensure that it is only
# loaded once.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
  config.run_all_when_everything_filtered = true
  config.filter_run :focus
  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = 'random'
end

似乎有一半的时间,当我运行 rspec 时,所有测试都通过了。

如果有人好奇,我的存储库在这里:https://github.com/NullVoxPopuli/skinny_controllers

最佳答案

这是我最终测试 Rails 应用程序的方式 here is the gem where I have a dummy rails app

无论您想测试 Rails 应用程序的规范文件是什么,您都需要一个新文件,我将其命名为 rails_helper.rb

require 'rails/all'

require 'factory_girl'
require 'factory_girl_rails'
require 'rspec/rails'

require 'support/rails_app/config/environment'

ActiveRecord::Migration.maintain_test_schema!

# set up db
# be sure to update the schema if required by doing
# - cd spec/support/rails_app
# - rake db:migrate
ActiveRecord::Schema.verbose = false
load 'support/rails_app/db/schema.rb' # db agnostic

require 'support/rails_app/factory_girl'
require 'spec_helper'

然后在 spec/support 文件夹中,在那里生成您的 Rails 应用程序。我的位于:'spec/support/rails_app'。在这里,与默认脚手架生成的内容相比,rails 应用程序可以非常精简。

我确实必须在 config/environment.rb 中对应用程序进行更改

# Load the Rails application.
require File.expand_path('../application', __FILE__)

# gem should be required here
require 'skinny_controllers' 

# Initialize the Rails application.
Rails.application.initialize!

对于database.yml,

default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

test:
  <<: *default
  database: db/test.sqlite3

如果需要,可以将数据库设置更改为使用内存中的 sqlite。

希望就是这样。如果我遗漏了什么,我会更新这个答案。

关于ruby-on-rails - 编写 gem 时如何测试虚拟 Rails 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33766630/

相关文章:

ruby-on-rails - 无法使用自定义设计策略

ruby-on-rails - Rails 形式的 f.input 有什么作用?

node.js - Node js : I to get the output of an (child) exec and set a variable to be retuned?

php - 如何在 XAMPP 上测试响应式网页设计?

ruby-on-rails - RoR Mailboxer Monkey Patch 只工作了几次

ruby-on-rails - 如何在 Twitter Bootstrap 图标周围设置边框?

ruby-on-rails - Rails/Ruby 的痛苦 - 如何检查 gem 是否基于 UNIX/类 UNIX?

testing - 谷歌模拟 - 使用::测试::An()

ruby - 短路 Ruby `begin ... end` block 的正确习惯用法是什么?

ruby - `@` 在 YAML 中意味着什么(导致 Psych::SyntaxError)?