...Remove default rules BrowserConsole.ignore_nothing # Add your own rules BrowserConsole.ignore(...) BrowserConsole.ignore(...) Automatic checking in RSpec feature specs In your spec_helper.rb you may configure to automatically check for errors after every...

...A compromise might be to only check the console when the spec has ended: RSpec.configure do |config| config.after do BrowserConsole.assert_no_errors! end end Ignoring JavaScript errors for a spec...

github.com

There are three ways to define your own RSpec matchers, with increasing complexibility and options: 1) Use RSpec::Matchers.define RSpec::Matchers.define :be_a_multiple_of do |expected| match do |actual...

...when the code is loaded. We have a note on where to put custom RSpec matchers. The shown approach can also be used to compose a custom matcher from existing...

RSpec allows defining methods inside describe/context blocks which will only exist inside them. However, classes (or any constants, for that matter) will not be affected by this. If you define...

...them in your specs, they will exist globally. This is because of how RSpec works (short story: instance_eval). Negative example: describe Notifier do class TestRecord < ApplicationRecord # DO NOT do...

...validations can be tested with less code The shoulda-matchers gem gives you some RSpec matchers to test the application of standard Rails validations. Under the hood should-matchers uses...

makandra dev
relishapp.com

...from ActiveRecord are verified. Note that this is enabled by default when you are using rspec-rails along with verifying doubles. So if you really need to verify some dynamically...

...doubles for the affected specs. Verifying doubles in RSpec 2 For older RSpecs there is rspec-fire...

relishapp.com

...to return true, got false (Spec::Expectations::ExpectationNotMetError) Solution That can be fixed easily. RSpec expectations allow you to pass an error message like this: expect(User.last).to be_present...

...gtm.dom"}, {"event"=>"gtm.load"}] (Spec::Expectations::ExpectationNotMetError) And the best thing: Since you are using RSpec expectations in your Cucumber steps most of the time, you can do the same there...

relishapp.com

In RSpec you can tag examples or example groups with any tags you like simply by saying describe ReportCreator, slow: true do # .. end describe ReportCreator do it 'generates reports', slow...

# ... end end You can then only run examples with these tags. rspec --tag slow rspec -t slow # Using the parallel_tests gem rake "parallel:spec[,,--tag slow]"

RSpec >= 3.3 added aggregate_failures, which allows multiple failures in an example and list them all, rather than aborting on the first failure. This can be used:

...obvious (and not yet documented) it is written down in this card. In RSpec 3 :each has the alias :example and :all the alias :context. Example RSpec.configure do |config| config.before...

...your "Background" steps How to set up database_cleaner for Rails with Cucumber and RSpec Before all in database transactions will fail

You can use RSpec::Matchers.define_negated_matcher to define a negated version of an existing matcher. This is particularly useful in composed matcher expressions or to create more expressive and...

...all". Prefer using attributes in this case: have_radio_button(label, active: true). Examples RSpec::Matchers.define_negated_matcher :not_change, :change describe 'A negated matcher' do it 'can be used...

RSpec allows you to mark a single Example/ExampleGroup so that only this will be run. This is very useful when using a test runner like guard. Add the following config...

...to spec/spec_helper.rb: RSpec.configure do |config| # These two settings work together to allow you to limit a spec run # to individual examples or groups you care about by tagging them with...

rspec.info

rspec >= 3.1 brings a method and_wrap_original. It seems a bit complicated at first, but there are use cases where it helps to write precise tests. For example it...

...that will only be created when your code is called. If you have older rspec, you could use expect_any_instance_of, but with the drawback, that you can't...

...receive(:foo).with do |received_object| expect(received_object.first_name).to eq('Hans') ... end See Rspec: Complex argument expectations for should_receive for more details...

rspec.info

In modern default RSpec configurations, your tests are usually run in random order. This helps to detect "flickering" tests that only fail when run in a certain order.

...tests to fail later. The hard part is to find the offending test. Enter rspec --bisect: Say you have a flickering test that passes on its own, but you just...

makandra dev
rubydoc.info

CarrierWave comes with some RSpec matchers which will make testing more comfortable. Let's say you have an Uploader like this: class MyUploader < CarrierWave::Uploader::Base include CarrierWave::MiniMagick

...you can add tests to check the dimensions of the several versions by writing RSpec.describe Movie, type: :model do include CarrierWave::Test::Matchers describe '#poster' do before do @movie = create...

relishapp.com

When you are using lambdas in RSpec to assert certain changes of a call, you know this syntax: expect { playlist.destroy }.to change { Playlist.count }.by(-1) While you can define multiple...

...performance or for the sake of mental overhead. Multiple expectations on the same subject RSpec allows chaining expectations simply by using and. expect { playlist.destroy } .to change { Playlist.count }.by(-1)

When two classes implement the same behavior (methods, callbacks, etc.), you should extract that behavior into a trait or module...

relishapp.com

...anonymous class internally, this will include the matcher for this example group only. require 'rspec/expectations' module MyHelpers extend RSpec::Matchers::DSL matcher :be_just_like do |expected| match {|actual| actual...

When ending a Selenium test Capybara resets the browser state by closing the tab, clearing cookies, localStorage, etc.

RSpec provides a nice diff when certain matchers fail. Here is an example where this diff is helpful while comparing two hashes: {a:1}.should match(a:1, b...

Unfortunately, this diff is not as clever as it would need to. RSpec's instance_of matchers will look like errors in the diff (even if they are...

When you mocked method calls in RSpec, they are mocked until the end of a spec, or until you explicitly release them. You can use RSpec::Mocks.with_temporary_scope to...

...all mocks applied inside a block to be released when the block ends. Example: RSpec::Mocks.with_temporary_scope do allow(Rails).to receive(:env).and_return('production'.inquiry) puts Rails.env...

Custom matchers are a useful RSpec feature which you can use...

...to DRY up repetitive expectations in your specs. Unfortunately the default directory structure generated by rspec-rails has no obvious place to put custom matchers or other support code.

...of a matcher that memoizes its observation (in #actual_state) and cannot be retried: RSpec::Matchers.define :have_state do |expected_state| match do |element| actual_state(element) == expected_state

RSpec Rails can automatically mix in different behaviors to your tests based on their type tag, for example enabling you to call get and post in specs with the tag...