Our preferred way of testing ActiveRecord is to simply create/update/destroy the record and then check if the expected behavior has...

github.com

When handling nested hashes the RSpec output is often hard to read. Here the gem super_diff could help. Add super_diff to your project Add super_diff to your...

general_kenobi: 'Hello there!' } } } } } } } but he is actually in level 3. Then RSpec will output the diff this way: expected: {:level_1=>{:level_2=>{:level_3=>{:level...

thegnar.com

...useful because they become quite convenient when used with Capybara::Node::Finders and Capybara::RSpecMatchers. This allows to wirte view unit specs as you can isolate specific parts of the...

...link_element).to have_selector('.nav-link') Info Even though require 'capybara/rspec' includes Capybara::RSpecMatchers by default, such that rendered is converted once one of Capybara's matchers is called...

...change global state, you should not need to care about the order in which RSpec processes your .rb files. However, in some cases you might want to know.

...order random on the command line, or by putting it into your project's .rspec file. Note that both switches also affect the order the tests inside each file are...

github.com

When an Rspec example fails, I usually investigate by running that example again using rspec . However, this does not work with shared examples, since Rspec doesn't know in which...

...When the shared example "it does something" fails, you can run it like this: rspec spec/models/my_model_spec.rb --example 'something' If you are using Rspec >= 3.3 you could also run a shared...

If you expect method calls in RSpec 3, be aware that the argument matchers use very liberal equality rules (more like === instead of ==). For example: expect(subject).to receive(:foo...

...file path with the line number of the example and pass it to the RSpec binary: bin/rspec spec/models/user_spec.rb:30 (multiple line numbers work as well: :30:36:68). Another is...

...then run the 2nd example within (it 'is nil for a new user'). Related RSpec: Running examples by name RSpec: Run a single spec (Example or ExampleGroup) (fit, xit...

...your tests, you can improve readability by extracting the group into its own matcher. RSpec makes this easy by allowing matchers to call other matchers. Example The following test checks...

...implementing the match and end_with matchers, our new matcher can simply use expect: RSpec::Matchers.define :be_shouting do match do |string| expect(string).to_not match(/[a-z]/)

...your tests multiple times, it might be a good idea to extract a custom RSpec matcher. This not only tidies up your own code, but also makes it easier to...

expect(page).to have_text(text) end end end See also RSpec: Composing a custom matcher from existing matchers RSpec: Scoping custom matchers to example groups

...path = "tmp/test#{ENV['TEST_ENV_NUMBER']}/cache" FileUtils::mkdir_p(path) path end end RSpec.configure do |config| config.include CachingHelpers end 3. Now stub the Rails cache and clear it before...

RSpec.describe SomeClass let(:file_cache) { ActiveSupport::Cache.lookup_store(:file_store, file_caching_path) } let(:cache) { Rails.cache } before do allow(Rails).to receive(:cache).and_return(file_cache) Rails.cache.clear...

...method to activate VCR for a spec by simply tagging it with :vcr. For RSpec3 the code looks almost the same with a few minor changes. If you have the...

...spec/support/vcr.rb VCR.configure do |c| c.cassette_library_dir = Rails.root.join("spec", "vcr") c.hook_into :webmock end RSpec.configure do |c| c.around(:each, :vcr) do |example| name = example.metadata[:full_description].split(/\s+/, 2).join...

...some cookie dependent behavior as a request, helper or model spec too. Since the rspec documentation on testing cookies is rather sparse and only focuses on controller specs, which recommended...

...spec with cookies. Warning Note that using request.cookies and response.cookies is not recommend by RSpec anymore as can be read in the documentation link above. Model specs Whenever you have...

In RSpec 2 shared_examples_for can have parameters. You can simply hand over arguments from it_behaves_like: shared_examples_for 'string equaling another string' do |expected...

...some string' do subject { 'foo' } it_behaves_like 'string equaling another string', 'foo' end RSpec 1 Use it_should_act_like from our spec_candy.rb (note the /behave/act/). It allows you...

While verifying doubles in RSpec is a good default, it is limited in the amount of methods it actually is able to verify. The background is that RSpec can't...

...a known issue for the usage of helper_method and also the reason why RSpec >= 3.6 (see "Mocks: without_partial_double_verification") added without_partial_double_verification.

hash_including( id: 1, thread: hash_including(id: 1) ) ) end end Note: RSpec will highlight array_including and hash_including matches with red even they should be green...

The RSpec matcher tests if two HTML fragments are equivalent. Equivalency means: Whitespace is ignored Types of attribute quotes are irrelevant Attribute order is irrelevant Comments are ignored

gem 'compare-xml' Here is the matcher: require 'nokogiri' require 'compare-xml' RSpec::Matchers.define :match_html do |expected_html, **options| match do |actual_html| expected_doc = Nokogiri::HTML5.fragment...

RSpec lets you define the arguments with which you expect a method to be invoked: subject.should_receive(:say).with('hello') Sometimes you don't care about the exact arguments. For...

...such cases RSpec comes with argument constraints like anything or hash_including: subject.should_receive(:update_attributes).with(hash_including(:message => 'hi world')) You can go even further and use any...

github.com

...the constant has been loaded, a module is automatically created with the name. Since RSpec does no autoloading, it will create a SomeClass module by itself. This is arguably a...

stub_const "#{SomeClass}::CONST", 'test' This will invoke Rails' autoloading and fix RSpec's behavior for you...

When tests might not run with skipping RSpec in the RSpec.describe failing with the error undefined method 'describe' for main:Object this card will help you out!

...like describe is exposed globally by default. Therefore it is not necessary to write Rspec.describe. However, there is a config option to disable this beavior, which also disables the old...

The attached RSpec matcher exist_in_database checks if a given record still exists in the database and has not been destroyed: describe Ticket do describe '.purge_expired' do

rspec.info

...You should always try to fix those instead of rerunning them regularly. Setup Configure RSpec to persist the result of your test runs to a file. This is necessary to...

...status_persistence_file_path = 'spec/examples.txt' Rerun all failed examples using --only-failures bundle exec rspec --only-failures (or g rs --only-failures with geordi) will rerun all examples which failed...

...in the current branch since master: git diff --name-only master -- ./spec | xargs -I{} rspec {} If you have several spec folders add them for path parameter after ./spec accordingly.

If an view spec crashes due to undefined helper methods, you can enable this option: # config/application.rb config.action_controller.include_all_helpers = true...

In contrast to RSpec's included allow_value matcher, the attached matcher will also work on associations, which makes it ideal for testing assignable_values. Usage example describe Unit do...

...building, other_building) expect(unit.assignable_buildings).not_to include(unauthorized_building, nil) end ... See RSpec: Where to put custom matchers and other support code