[英]How do you test a dummy rails app when writing a gem?
I'm writing a gem that integrates with rails, and I want to be able to test a dummy app with rspec inside my gem's test suite. 我正在编写一个与rails集成的gem,我希望能够在我的gem测试套件中使用rspec测试一个虚拟应用程序。
The issue comes up when I test if my dummy rails app loads / a couple modules via rspec spec/integration/rails/load_path_spec.rb
当我通过
rspec spec/integration/rails/load_path_spec.rb
测试我的虚拟轨道应用程序是否加载/几个模块时rspec spec/integration/rails/load_path_spec.rb
So far, this is what I have: 到目前为止,这就是我所拥有的:
# 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!
Me test looks like this: 我测试看起来像这样:
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 looks like this: 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'
And spec_helper looks like this: 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
it seems like, half of the time, when I run rspec
, all of the tests pass. 似乎,有一半时间,当我运行
rspec
,所有测试都通过了。
If anyone is curious, my repository is here: https://github.com/NullVoxPopuli/skinny_controllers 如果有人好奇,我的存储库就在这里: https : //github.com/NullVoxPopuli/skinny_controllers
Here is how I ended up testing a rails app here is the gem where I have a dummy rails app 以下是我最终测试rails应用程序的方法, 这里是我有一个虚拟rails应用程序的宝石
In whatever spec files you want to test your rails app, you'll want to require a new file, which I called rails_helper.rb
在你想要测试rails应用程序的任何spec文件中,你需要一个新文件,我称之为
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'
And then in the spec/support
folder, generate your rails app there. 然后在
spec/support
文件夹中,在那里生成rails应用程序。 Mine is at: 'spec/support/rails_app'. 我的位置是:'spec / support / rails_app'。 In here, the rails app can be very slimmed down compared to what the default scaffold generates.
在这里,与默认支架生成的相比,rails app可以非常精简。
I did have to make a change to the app, in config/environment.rb
我确实需要在
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!
And for the database.yml, 而对于database.yml,
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/test.sqlite3
The db setting could probably be changed to use in-memory sqlite, if one so desired. 如果需要,可以将db设置更改为使用内存中的sqlite。
Hopefully that's it. 希望就是这样。 If I missed anything, I'll update this answer.
如果我遗漏了什么,我会更新这个答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.