![](/img/trans.png)
[英]rails: how to supply a model with gem, and how to test rails gem without dummy app
[英]How do you test a dummy rails app when writing a gem?
我正在編寫一個與rails集成的gem,我希望能夠在我的gem測試套件中使用rspec測試一個虛擬應用程序。
當我通過rspec spec/integration/rails/load_path_spec.rb
測試我的虛擬軌道應用程序是否加載/幾個模塊時rspec spec/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應用程序的方法, 這里是我有一個虛擬rails應用程序的寶石
在你想要測試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'
然后在spec/support
文件夾中,在那里生成rails應用程序。 我的位置是:'spec / support / rails_app'。 在這里,與默認支架生成的相比,rails app可以非常精簡。
我確實需要在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
如果需要,可以將db設置更改為使用內存中的sqlite。
希望就是這樣。 如果我遺漏了什么,我會更新這個答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.