1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

vscodeでRSpec

Last updated at Posted at 2019-03-11

覚え書き

エディタ

・VScode

手順

作業ディレクトリを作る

    $ bundle init


rspecのジェムを読み込む

Gemfile.
# gem 'rails'
gem 'rspec'

    $ bundle install --path vendor/bundle


テストされるコードを置くフォルダ(例:libフォルダ)
と、テストコードを置くフォルダを作る(specフォルダ)

FizzBuzz テストされるフォルダ

first.rb
class FizzBuzz

    def get_fizz_buzz_text(number)
      if number == 15 || number == 30
        "FizzBuzz"
      elsif number % 5 == 0
        "Buzz"
      elsif number % 3 == 0
        "Fizz"
      else
        number.to_s
      end
    end
  end

FizzBuzz テストコードを置くフォルダ

first_spec.rb
RSpec.describe FizzBuzz do
  let(:fizz_buzz) { FizzBuzz.new }
    it "1の時1が来る" do
      result = fizz_buzz.get_fizz_buzz_text(1)
      expect(result).to eq "1"
    end
  end
end

bundle exec rspec テスト実行

    $ bundle exec rspec
An error occurred while loading ./spec/first_spec.rb.
Failure/Error: require 'spec_helper'

LoadError:
  cannot load such file -- spec_helper

'spec_helper'が必要

rspecのhelper

specフォルダ(テストデータのあるフォルダの中)に、spec_helper.rbというファイルを作って中にコピペする
こういった内容を他のテストファイルで読み込めればいいので名前はなんでもいいはず。
specのhelperということなのでspecが含まれる(?)ファイルのテストコードを動かす力を持っている(?)という理解
ruby_spec1.rbqiitaspec.rbとかいうファイルは動かないが
aaaaaaaa_spec.rbはうごく

spec_helper.rb
RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.shared_context_metadata_behavior = :apply_to_host_groups
  config.filter_run_when_matching :focus

  config.example_status_persistence_file_path = "spec/examples.txt"

  config.disable_monkey_patching!

  config.warnings = true

  if config.files_to_run.one?
    config.default_formatter = "doc"
  end

  config.profile_examples = 10

  config.order = :random

  Kernel.srand config.seed
  Dir[File.join(File.dirname(__FILE__), "../lib/**/*.rb")].each { |f| require f }
end

FizzBuzz テストコード 完成

first_spec.rb
require 'spec_helper'

RSpec.describe FizzBuzz do
  let(:fizz_buzz) { FizzBuzz.new }
  context 'その他のとき' do
    it "1の時1が来る" do
      expected = "1"
      result = fizz_buzz.get_fizz_buzz_text(1)
      expect(result).to eq expected
    end
  end
  context '3の倍数のとき' do
    let(:fizz_test_cases) do
      [ 3, 6 ]
    end
    it "3の時Fizzが来る" do
      expected = "Fizz"
      fizz_test_cases.each do |test_case|
        result = fizz_buzz.get_fizz_buzz_text(test_case)
        expect(result).to eq expected
      end
    end
  end
  context '5の倍数のとき' do
    let(:buzz_test_cases) do
      [ 5, 10 ]
    end
    it "5の時Buzzが来る" do
      expected = "Buzz"
      buzz_test_cases.each do |test_case|
        result = fizz_buzz.get_fizz_buzz_text(test_case)
        expect(result).to eq expected
      end
    end
  end
  context '3と5の倍数のとき' do
    it "15の時FizzBuzzが来る" do
      result = fizz_buzz.get_fizz_buzz_text(15)
      expected = "FizzBuzz"
      expect(result).to eq expected
    end
    it "30の時FizzBuzzが来る" do
      result = fizz_buzz.get_fizz_buzz_text(30)
      expected = "FizzBuzz"
      expect(result).to eq expected
    end
  end
end

bundle exec rspec

結果


Randomized with seed 28943
.....
Top 5 slowest examples (0.00164 seconds, 14.8% of total time):
  FizzBuzz 5の倍数のとき 5の時Buzzが来る
    0.00124 seconds ./spec/first_spec.rb:28
  FizzBuzz 3と5の倍数のとき 15の時FizzBuzzが来る
    0.00012 seconds ./spec/first_spec.rb:37
  FizzBuzz その他のとき 1の時1が来る
    0.0001 seconds ./spec/first_spec.rb:6
  FizzBuzz 3と5の倍数のとき 30の時FizzBuzzが来る
    0.00009 seconds ./spec/first_spec.rb:42
  FizzBuzz 3の倍数のとき 3の時Fizzが来る
    0.00009 seconds ./spec/first_spec.rb:16

Finished in 0.01108 seconds (files took 0.15655 seconds to load)
5 examples, 0 failures

Randomized with seed 28943

thank you にゃー

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?