Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to test Rails engine generator with RSpec

How to test Rails engine generator with RSpec

shinjuku.rb #26(2015/06/24)

Yuichi Goto

June 24, 2015
Tweet

More Decks by Yuichi Goto

Other Decks in Technology

Transcript

  1. About me [1] pry(main)> me => { twitter:'@_yasaichi', github: 'yasaichi',

    splatoon: nil, blog: 'http://web-salad.hateblo.jp' }
  2. RSpec + Gem • RSpec͸GeneratorͷςετΛඪ४Ͱ
 αϙʔτ͍ͯ͠ͳ͍ͷͰɺGemΛ࢖͏ • generator_spec, ammeterͷ2୒ʁ •

    ͲͪΒ΋GitHubͰ˒50͘Β͍Ͱɺ͋·Γ
 ੝Γ্͕ΓΛײ͡ΒΕͳ͍ ٩(๑`н´๑)۶
  3. Usage RSpec.describe MyGem::Generators::InstallGenerator, type: :generator do destination File.expand_path('../../tmp', __FILE__) before

    do prepare_destination # initialize ../../tmp end describe 'generated files' do before do run_generator # run "rails g my_gem:install" end # write test cases here end after(:context) do FileUtils.rm_rf(destination_root) end end
  4. Matchers # ଞʹ be_a_migration, have_method ͕͋Δ # ৄ͘͠͸ https://github.com/alexrothenberg/ammeter Λࢀরͷ͜ͱ

    describe 'generated files' do before do run_generator # run: rails g my_gem:install end describe 'config/initializers/my_gem.rb' do subject { file('config/initializers/my_gem.rb') } it { is_expected.to exist } it { is_expected.to have_correct_syntax } it { is_expected.to contain /\AMyGem\.configure do \|config\|/ } end end