Created
March 3, 2015 15:55
-
-
Save afair/fa5b81669d1f31f3f272 to your computer and use it in GitHub Desktop.
RSpec3 Template Spec and Reference by Example
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
require 'spec_helper' | |
describe Widget do | |
# Setup, Exercise, Verify, Teardown! | |
describe '#instance_method' do | |
subject { buid_stubbed(:widget) } | |
it 'works' do | |
expect(subject.name).to be_nil | |
end | |
context 'state' do | |
it { is_expected.to be_nil } | |
end | |
end | |
end | |
################################################################################ | |
describe RSpec3Spec do | |
include CustomMatchers | |
describe 'before and after hooks' do | |
before(:suite || :example || :context) { } | |
after( :suite || :example || :context) { } | |
around( :suite || :example || :context) do |example| | |
example.run | |
end | |
end | |
def my_helper | |
:response | |
end | |
describe '.method' do # .classmethod ::method #instancemethod | |
context 'when state' do # valid, logged in, not found, errrors, etc. | |
subject { Model.new } # Unnamed subject | |
subject(:other) { Model.new } # Names subject, or reference with: other.x | |
let(:widget) { FactoryGirl.create :widget } # Assigned caches widget var (method) | |
it { is_expected.to be_empty } # tests what unnamed subject returns | |
it 'does something', focus:true do | |
expect(other.status).to eq('value') # Names Subject | |
expect(widget.status).to eq('value') # Let is llike subject, but cached | |
expect(widget.status).not_to eq('value') | |
expect(widget.status).to start_with("foo").and end_with("bazz") # .or | |
expect(widget.status).to eq('value'), "customized message" | |
expect(assigns('instance_var')).to eq('value')# retrives instance variable? | |
end | |
end | |
end | |
it "expect object predicates" do | |
expect(actual).to eql(value_and_type) | |
expect(actual).to equal(same_as_this_object) | |
expect(actual).to be_truthy # be_false be_falsy be_nil | |
expect(actual).to be >= 10 # < <= >= > | |
expect(actual).to be_between(min,max).inclusive # .exclusive | |
expect(actual).to be_within(delta).of(expected) | |
expect(actual).to match(/regex/) | |
expect(actual).to start_with str_or_array # end_with | |
expect(actual).to ba_a Widget # be_: an a_kind_of an_instance_of | |
expect(actual).to respond_to(:method, :method2) # Duck typing | |
expect(actual).to repsond_to(:foo).with(2).arguments | |
expect(actual).to match_array [a,b,c] # Ignores order | |
expect(actual).to contain_exactly(a,b,c) # Ignores order | |
expect(actual).to have(3).things # have_at_least have_at_most | |
expect(actual).to include x # "substr", [2,1] | |
expect(actual).to cover 3,4,5 # coverage for a range | |
expect(actual).to satisfy {|v| v % 2 == 0 } | |
expect(actual).to be_xxx # passes if actual.xxx? | |
expect(actual).to have_xxx(:arg) # passes if actual.has_xxx?(:arg) | |
end | |
it "expect block predicates" do | |
expect { actual }.to raise_error | |
expect { actual }.to raise_error RuntimeError | |
expect { actual }.to raise_error "exact message" | |
expect { actual }.to raise_error(/message regex/) | |
expect { actual }.to raise_error RuntimeError, "exact message or regex" | |
expect { actual }.to throw_symbol :sym | |
expect { actual }.to throw_symbol :sym, "arg" | |
expect { |b| 5.tap(&b) }.to yield_control | |
expect { |b| 5.tap(&b) }.to yield_with_args "foo", /bar/ | |
expect { |b| 5.tap(&b) }.to yield_with_no_args | |
expect { |b| 5.tap(&b) }.to yield_successive_args "foo", "bar" | |
expect { actual }.to change(object, :value).from(0).to(1) | |
expect { actual }.to change(object, :value).by(delta) | |
expect { actual }.to change(object, :value).by_at_least(min) # _most(max) | |
expect { actual }.to output("string").to_stdout # to_stderr | |
end | |
############################################################################## | |
# http://matchers.shoulda.io/ | gem 'shoulda-matchers', require: false | |
############################################################################## | |
it 'shoulda matchers' do | |
expect(subject).to validate_presence_of :email | |
expect(subject).to validate_xxxx_of :attribute # exclusion inclusion length numericality uniqueness | |
expect(subject).to xxxx :args | |
# accept_nested_attributes_for belong_to define_enum_for have_and_belong_to_many | |
# have_db_(column|index) have_(many|one|readonly_attribute) serialize | |
# filter_patam redirect_to render_template render_wth_layout rescue_from respond_with | |
# route set_session set_flash use_(afer|around|before)_action | |
end | |
it { is_expected.to allow_value("closed") } # validates_format_of | |
############################################################################## | |
# MOCKSES AND STUBSES: http://rspec.info/documentation/3.2/rspec-mocks/ | |
############################################################################## | |
it "stubs and mocks" do | |
before { allow(Resource).to receive(:where).with(id:id).and_return(false) } | |
receiver = double("receiver", method: "return value") | |
expect(receiver).to receive(:message) | |
receiver.message | |
end | |
############################################################################## | |
# DEFINED MATCHER | |
############################################################################## | |
it { is_expected.to be_a_multiple_of(3) } | |
RSpec::Matches.define :be_a_multiple_of do |expected| | |
match {|actual| actual % expected == 0 } | |
end | |
############################################################################## | |
# SHARED EXAMPLE GROUP | |
############################################################################## | |
it_behaves_like "a cleaner" | |
shared_examples "a cleaner" do | |
let(:foo) { described_class.new([1,2,3]) } | |
it ".cleans" do | |
expect(foo.clean.size).to eq(0) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment