...or false. That's why it has been renamed to be_truthy in recent RSpec versions. The same thing holds for be_false, which actually checks if a value is...

If you want to check for true or false in RSpec 2, write this instead: value.should == true value.should == false If you want to check for true or false...

Inside before :each blocks you can refer to variables that you introduce via let later on. They do not need...

When using the resource_controller gem you often hook onto events like this: update.before do do_something end

...deal.total_price).to eq BigDecimal(1200.99) If you don't like the syntax, our rspec_candy gem has a matcher that will compare Fixnums (integers), Floats and BigDecimals with each...

relishapp.com

You can configure RSpec 3.3+ to raise an error when attempting to stub or mock a non-existing method. We strongly recommend to do this as non-verified stubs are...

You can enable this behavior by adding the following to your spec_helper.rb: RSpec.configure do |config| config.mock_with :rspec do |mocks| mocks.verify_partial_doubles = true end end

This works in modern RSpecs (RSpec >= 2.x) and Cucumbers: rspec spec/models/node_spec.rb:294:322 cucumber features/nodes.feature:543:563:579 Also your features should be shorter than that...

Sometimes you need a piece of code to do something different for specs than for features. If you don't...

makandra dev

Never use raise_error without specifying the Error you expect. expect { do_a_lot_of_complicated_stuff }.to raise_error...

betterspecs.org

betterspecs.org is a documentation on how to write better RSpec tests. Note that there are also other approaches like The Self-Contained Test, which is complementary to the dry-approches...

Reading user input in console applications is usually done using Kernel#gets. Stubbing that can be a bit hairy.

idolhands.com

Passing the --profile flag to RSpec produces some additional output, namely the running times of the ten slowest examples in your specs...

In a Rails application, *_spec.rb files get special treatment depending on the file's directory. E.g. when you put a...

To only run a single describe/context block in a long spec, you can say spec spec/models/note_spec.rb:545 ... where the describe...

web.archive.org

...original implementation of stubbed methods with unstub: object.stub(:foo => 'bar') # ... object.unstub(:foo) In recent RSpecs double is the preferred way to create a mock object (stub and mock are aliases...

If the argument list is the same every time: expect(object).to receive(:foo).with('argument').and_return('response 1...

This finally works: User.any_instance.should_receive(...) as does User.any_instance.stub(...) Note: You won't have RSpec 2.6 if you're still working on Rails...

Never name your shared example group *_spec.rb. Otherwise rspec will try to load your example group as a spec and you will get the error above...

makandra dev
gist.github.com

The attached RSpec matcher allows for comfortably testing delegation. Examples describe Post do it { should delegate(:name).to(:author).with_prefix } # post.author_name it { should delegate(:month).to(:created_at...

...to(:created_at) } end Credits go to txus. See the attached link for an RSpec 2+ version...

jeffkreeftmeijer.com

Back when Steak was first released, Capybara didn’t have any of the nice RSpec helpers it does now. A lot has changed since. Besides the helpers, it got its...

...own RSpec acceptance testing DSL recently, essentially eating Steak’s functionality and turning it into a complete acceptance testing solution (on top of RSpec...

To test whether a hash includes an expected sub-hash: expect(user.attributes).to match(hash_including('name' => 'Bruce Wayne'))

When a spec only runs when it is called directly, but not as part of the whole test suite, make...

...arrays have the same elements regardless of order, you can use the =~ matcher in RSpec < 2.11: actual_array.should =~ expected_array If either side is an ActiveRecord scope rather than an array...

...first, since =~ does not play nice with scopes: actual_scope.to_a.should =~ expected_scope.to_a If you use RSpec >= 2.11 we recommend using the match_array or contain_exactly matchers instead of =~.