Last active
August 31, 2018 12:38
-
-
Save Epigene/bcb6ca09a28808e214177407f0fee5d3 to your computer and use it in GitHub Desktop.
Ruby (predominantly RSpec) snippets for VSC.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"scope spec template": { | |
"prefix": "scope_spec", | |
"body": [ | |
"describe \".SCOPE_NAME\" do", | |
" subject(:collection) { described_class.SCOPE_NAME }", | |
"", | |
" context \"when instantiating actual records\", :slow do", | |
" let!(:match) { create(:FACTORY, :TRAIT) }", | |
" let!(:counter) { create(:FACTORY) }", | |
"", | |
" it \"collects TODO records\" do", | |
" expect(collection.pluck(:id)).to contain_exactly(match.id)", | |
" end", | |
" end", | |
"", | |
" context \"when only verifying scope's SQL\" do", | |
" subject { super().to_sql }", | |
"", | |
" let(:sql) { \"some sql\" }", | |
"", | |
" it { is_expected.to eq(sql) } ", | |
" end", | |
"end" | |
], | |
}, | |
"aggregate failure it block": { | |
"prefix": "agg", | |
"body": [ | |
"it \"has these $0\", :aggregate_failures do", | |
"", | |
"end", | |
], | |
}, | |
"spring rspec line": { | |
"prefix": "sr", | |
"body": [ | |
"# spring rspec $0", | |
], | |
}, | |
"frozen string literal pragma": { | |
"prefix": "frozen", | |
"body": [ | |
"# frozen_string_literal: true", | |
], | |
}, | |
"transient block": { | |
"prefix": "tt", | |
"body": [ | |
"transient do", | |
"", | |
"end", | |
], | |
}, | |
"described class": { | |
"prefix": "dc", | |
"body": [ | |
"described_class" | |
], | |
}, | |
"described instance": { | |
"prefix": "di", | |
"body": [ | |
"described_instance" | |
], | |
}, | |
"cache block": { | |
"prefix": "cache", | |
"body": [ | |
"Rails.cache.fetch(:some_key, expires_in: 24.hours) do" | |
" $0code_that_produces_the_value_if_it_is_absent" | |
"end" | |
], | |
}, | |
"memoization using defined?": { | |
"prefix": "memo", | |
"body": [ | |
"return @?? if defined?(@??)", | |
"$0" | |
], | |
}, | |
"DB transaction block": { | |
"prefix": "t_b", | |
"body": [ | |
"ActiveRecord::Base.transaction do", | |
"$0", | |
"end" | |
], | |
}, | |
"begin-rescue block": { | |
"prefix": "begin", | |
"body": [ | |
"begin", | |
" $0", | |
"rescue", | |
" ", | |
"end" | |
], | |
}, | |
"after block": { | |
"prefix": "after", | |
"body": [ | |
"after do", | |
" $0", | |
"end" | |
], | |
}, | |
"before block": { | |
"prefix": "before", | |
"body": [ | |
"before do", | |
" $0", | |
"end" | |
], | |
}, | |
"before oneliner block": { | |
"prefix": "before_", | |
"body": [ | |
"before { $0 }", | |
], | |
}, | |
"scope definition": { | |
"prefix": "sco", | |
"body": [ | |
"scope :named, ->(argument) { where(attribute: argument) }" | |
], | |
}, | |
"sequence line": { | |
"prefix": "sequence", | |
"body": [ | |
"sequence(:email, 1) { |n| \"person\#{n}@example.com\" }" | |
], | |
}, | |
"describe block": { | |
"prefix": "desc", | |
"body": [ | |
"describe \"#$0\" do", | |
" subject(:) { }", | |
" ", | |
" context \"when \" do", | |
" ", | |
" it \" \" do", | |
" expect(0).to eq(1)", | |
" end", | |
" end", | |
"end" | |
], | |
}, | |
"context block": { | |
"prefix": "cont", | |
"body": [ | |
"context \"when \" do", | |
" it \" \" do", | |
" expect(0).to eq(1)", | |
" end", | |
"end" | |
], | |
}, | |
"it block": { | |
"prefix": "it", | |
"body": [ | |
"xit \"$0\" do", | |
" expect(0).to eq(1)", | |
"end" | |
], | |
}, | |
"controller render expectation lines": { | |
"prefix": "ecr", | |
"body": [ | |
"expect(controller).to receive(:render).with(", | |
"$0", | |
").once" | |
], | |
}, | |
"is_expected it block": { | |
"prefix": "it_", | |
"body": [ | |
"it { is_expected.to eq($0) }", | |
], | |
}, | |
"expect block": { | |
"prefix": "ex", "expect", | |
"body": [ | |
"expect($0).to match(1)" | |
], | |
}, | |
"expect receive block": { | |
"prefix": "exrec", | |
"body": [ | |
"expect(0).to(", | |
" receive(:).with().and_return()", | |
")" | |
], | |
}, | |
"let": { | |
"prefix": "let", | |
"body": [ | |
"let(:$0) do", | |
" ", | |
"end" | |
], | |
}, | |
"let!": { | |
"prefix": "let!", | |
"body": [ | |
"let!(:$0) do", | |
" ", | |
"end" | |
], | |
}, | |
"curly let": { | |
"prefix": "let_", | |
"body": [ | |
"let(:$0) { }", | |
], | |
}, | |
"curly let!": { | |
"prefix": "let!_", | |
"body": [ | |
"let!(:$0) { }", | |
], | |
}, | |
"FactoryBot create": { | |
"prefix": "Fac", | |
"body": [ | |
"create(:$0)" | |
], | |
}, | |
"FactoryBot build_stubbed": { | |
"prefix": "bs", | |
"body": [ | |
"build_stubbed(:$0)" | |
], | |
}, | |
"change block": { | |
"prefix": "change", | |
"body": [ | |
"change{ $0 }.from().to()" | |
], | |
}, | |
"contain_exactly": { | |
"prefix": "ce", | |
"body": [ | |
"contain_exactly($0)" | |
], | |
}, | |
"change block, full": { | |
"prefix": "exchange", | |
"body": [ | |
"expect{ }.to(", | |
" change{ $1 }.from().to()", | |
")" | |
], | |
}, | |
"gsub": { | |
"prefix": "gsub", | |
"body": [ | |
"gsub(%r'$0', '')" | |
], | |
}, | |
"each": { | |
"prefix": "each", | |
"body": [ | |
"each do |$0|", | |
" ", | |
"end" | |
], | |
}, | |
"do": { | |
"prefix": "do", | |
"body": [ | |
"do |$0|", | |
" ", | |
"end" | |
], | |
}, | |
"delegate": { | |
"prefix": "delegate", | |
"body": [ | |
"delegate :gifts, to: :friend, allow_nil: true, prefix: false" | |
], | |
}, | |
"trait block": { | |
"prefix": "trait", | |
"body": [ | |
"trait :$0 do", | |
" ", | |
"end" | |
], | |
}, | |
"short trait block": { | |
"prefix": "trait_", | |
"body": [ | |
"trait(:$0) {}" | |
], | |
}, | |
"timecop block": { | |
"prefix": "cop", | |
"body": [ | |
"Timecop.freeze(\"2018-01-10 12:00\".to_datetime) do", | |
" $0", | |
"end" | |
], | |
}, | |
"time lock block": { | |
"prefix": "lock", | |
"body": [ | |
"before { Timecop.freeze(\"2018-08-15 12:00\".to_datetime) }", | |
"after { Timecop.return }" | |
], | |
}, | |
"included block": { | |
"prefix": "included", | |
"body": [ | |
"included do", | |
" $0", | |
"end" | |
], | |
}, | |
"allow block": { | |
"prefix": "allow", | |
"body": [ | |
"allow($0).to receive(:).and_return()" | |
], | |
}, | |
"receive block": { | |
"prefix": "rec", "receive", | |
"body": [ | |
"receive(:$0).with().once.and_return()" | |
], | |
}, | |
"error spec block": { | |
"prefix": "err", | |
"body": [ | |
"expect{}.to raise_error(", | |
" RuntimeError, %r'message'", | |
")" | |
], | |
}, | |
"enum block": { | |
"prefix": "enum", | |
"body": [ | |
"enum some_type: {first: 0, second: 1}", | |
], | |
}, | |
"retryable block": { | |
"prefix": "retry", | |
"body": [ | |
"var = nil", | |
"Retryable.retryable(tries: 3, on: [ArgumentError, TimeoutError]) do", | |
" var = Model.where().first_or_create!", | |
"end" | |
], | |
}, | |
"method types block": { | |
"prefix": "method_types", | |
"body": [ | |
"#== Class Getters ==", | |
"#== Class Changers ==", | |
"#== Instance Getters ==", | |
"#== Instance Changers ==" | |
], | |
}, | |
"controller subject": { | |
"prefix": "mr", | |
"body": [ | |
"subject(:make_request) { get :some_action, params }", | |
], | |
}, | |
"general subject": { | |
"prefix": "sj", | |
"body": [ | |
"subject(:$0) { }", | |
], | |
}, | |
"before all setup and teardown": { | |
"prefix": "ball", | |
"body": [ | |
"before(:all) do", | |
"", | |
"end", | |
"", | |
"after(:all) { DatabaseCleaner.clean_with(:truncation) }", | |
], | |
}, | |
"any instance mock": { | |
"prefix": "any_in_of", | |
"body": [ | |
"allow_any_instance_of().to(", | |
" receive().and_return()", | |
")" | |
], | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment