Skip to content

Instantly share code, notes, and snippets.

@fobiasmog
Last active April 22, 2023 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fobiasmog/06e0c60d5761d0769ef850bc89638e8f to your computer and use it in GitHub Desktop.
Save fobiasmog/06e0c60d5761d0769ef850bc89638e8f to your computer and use it in GitHub Desktop.
Заметки по Rspec

Проверка того, что один массив хэшей содержится в другом (при этом порядок объявления ключей не важен, в отличии от использования .to eq)

RSpec.describe [{ a:  1 , b:  2 }, { a:  2 , b:  1 }, { c:  11 , d:  22 }] do
  let (:val) { [{ b:  1 , a:  2 }, { c:  11 , d:  22 }] }
  it  { is_expected.to include(*val) }
end

Вызов метода у subject для теста (its выпилили из rspec 3, https://gist.github.com/myronmarston/4503509, тут можно поставить гем https://github.com/dnagir/its)

subject { described_class.new }
describe '.some_method' do
  its(:some_method) { is_expected.to be_truthy }
end

Пропустить before hook для теста

before do |example|
  unless example.metadata[:skip_before]
    expect(attach_invoice).to receive(:update_assembly).and_call_original.once
  end
end
it '', skip_before: true do
  ...
end

Проверить наличие полей у модели

expect(User.last).to have_attributes(name: name)

Ссылка на доку по встроенным матчерам https://relishapp.com/rspec/rspec-expectations/v/3-10/docs/built-in-matchers

not_ matcher

RSpec::Matchers.define_negated_matcher :not_change, :change

.receive.and_...

U can wrap your call with .and_wrap_original # https://rspec.info/documentation/3.6/rspec-mocks/RSpec/Mocks/MessageExpectation.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment