IT

gem을 테스트하도록 RSpec 설정 (레일이 아님)

lottoking 2020. 6. 9. 07:49
반응형

gem을 테스트하도록 RSpec 설정 (레일이 아님)


rspec-rail 생성기가 추가되어 Rails 애플리케이션 테스트를 위해 RSpec을 설정하는 것은 매우 쉽습니다. 그러나 개발에서 gem을 테스트하기 위해 RSpec을 추가하는 것은 어떻습니까? 나는 보석상이나 그런 도구를 사용하지 않습니다. 방금 Bundler ( bundle gem my_gem)를 사용 하여 새 gem의 구조를 설정하고 * .gemspec을 수동으로 편집했습니다. 또한 s.add_development_dependency "rspec", ">= 2.0.0"gemspec에 추가 하고을 수행했습니다 bundle install.

RSpec을 작동시키기 위해 다음에 수행 할 멋진 튜토리얼이 있습니까?


현재 모범 사례와 일치하도록이 답변을 업데이트했습니다.

번 들러는 보석 개발을 완벽하게 지원합니다. 당신이 보석을 만드는 경우 에만 당신은 당신의 Gemfile에 있어야합니다 것은 다음과 같다 :

source "https://rubygems.org"
gemspec

이것은 Bundler가 gemspec 파일을 실행할 때 의존성을 찾도록 지시합니다 bundle install.

다음으로 RSpec이 gem의 개발 의존성인지 확인하십시오. gemspec을 다음과 같이 편집하십시오.

spec.add_development_dependency "rspec"

다음으로 다음 spec/spec_helper.rb과 같은 것을 작성 하고 추가하십시오.

require 'bundler/setup'
Bundler.setup

require 'your_gem_name' # and any other gems you need

RSpec.configure do |config|
  # some (optional) config here
end

처음 두 줄은 Bundler에게 gemspec 내부의 gem 만로드하도록 지시합니다. 자신의 컴퓨터에 자신 만의 gem을 설치하면, 스펙이 별도로 설치 한 버전이 아니라 현재 코드를 사용하게됩니다.

예를 들어 스펙을 작성하십시오 spec/foobar_spec.rb.

require 'spec_helper'
describe Foobar do
  pending "write it"
end

선택 사항 : .rspec기본 옵션에 파일을 추가하고 gem의 루트 경로에 넣으십시오.

--color
--format documentation

마지막으로 사양을 실행하십시오.

$ rspec spec/foobar_spec.rb

위의 Iain 솔루션은 훌륭합니다!

Rakefile도 원한다면 이것이 전부입니다.

require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

# If you want to make this the default task
task default: :spec

작업 정의에 선택적으로 전달할 수있는 다양한 옵션에 대해서는 RDoc for RakeTask확인하십시오 .


을 실행하여 rspec으로 새 gem을 생성 할 수 있습니다 bundler gem --test=rspec my_gem. 추가 설정이 없습니다!

나는 항상 이것을 잊었다. 여기에 구현되어 있습니다 : https://github.com/bundler/bundler/blob/33d2f67d56fe8bf00b0189c26125d27527ef1516/lib/bundler/cli/gem.rb#L36


저렴하고 쉬운 방법은 다음과 같습니다 (공식적으로 권장하지는 않음).

보석 루트에라는 디렉토리를 만들고, spec거기에 스펙을 넣으십시오. 이미 rspec을 설치했을 수도 있지만 그렇지 않은 경우 gem install rspecGemfile 및 번 들러를 잊어 버리십시오.

다음으로 사양을 작성하고 앱의 위치, 파일의 위치 및 테스트하려는 파일을 포함해야합니다 (의존성에 따라).

# spec/awesome_gem/awesome.rb
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
$: << File.join(APP_ROOT, 'lib/awesome_gem') # so rspec knows where your file could be
require 'some_file_in_the_above_dir' # this loads the class you want to test

describe AwesomeGem::Awesome do
  before do
    @dog = AwesomeGem::Awesome.new(name: 'woofer!')
  end
  it 'should have a name' do
    @dog.name.should eq 'woofer!'
  end
  context '#lick_things' do
    it 'should return the dog\'s name in a string' do
      @dog.lick_things.should include 'woofer!:'
    end
  end
end

터미널을 열고 rspec을 실행하십시오 :

~/awesome_gem $ rspec
..

Finished in 0.56 seconds
2 examples, 0 failures

If you want some .rspec options love, go make a .rspec file and put it in your gem's root path. Mine looks like this:

# .rspec
--format documentation --color --debug --fail-fast

Easy, fast, neat!

I like this because you don't have to add any dependencies to your project at all, and the whole thing remains very fast. bundle exec slows things down a little, which is what you'd have to do to make sure you're using the same version of rspec all the time. That 0.56 seconds it took to run two tests was 99% taken up by the time it took my computer to load up rspec. Running hundreds of specs should be extremely fast. The only issue you could run into that I'm aware of is if you change versions of rspec and the new version isn't backwards compatible with some function you used in your test, you might have to re-write some tests.

This is nice if you are doing one-off specs or have some good reason to NOT include rspec in your gemspec, however it's not very good for enabling sharing or enforcing compatibility.

참고URL : https://stackoverflow.com/questions/4398262/setup-rspec-to-test-a-gem-not-rails

반응형