RSpec's context (which is basically an alias for describe) takes over your whole application. No object may have its own context method, or you will always receive errors like...
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...
...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
...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.
...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...
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...
...seems to be obvious, but you can expect Rake tasks to be called in RSpec. it 'deletes all Users' do FactroyBot.create(:user) expect(Rake::Task['notify:critical_operation']).to receive...
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
...api.chucknorris.io/jokes/random') res = Net::HTTP.get_response(uri) expect(res.body).to include('Paul') end end rspec spec/chuck_norris_spec.rb . Finished in 0.00759 seconds (files took 0.49565 seconds to load) 1 example, 0 failures
RSpec is smart when using the include-matcher in combination with .not_to. One could assume that .not_to include(3, 4, 5) evaluates to: NOT( .to include...
...truth value. It instead negates the individual include-expectations for each argument. Proof describe 'RSpec' do it "doesn't use logical negation for include, exhibit A" do
...JSON.parse(User.last.to_json)['created_at'] #=> "2001-01-01T00:00:00.000+00:00" In RSpec you might want to use .to_json instead of .iso8601 to use the build-in...
tl;dr You can use ordered to ensure that messages are received in a specific order. Example expect(ClassA).to...
To check which elements an ActiveRecord relation contains use the contain_exactly matcher. describe User do let!(:admin) { create(:user...
The ActionDispatch module of Rails gives you the helper method flash to access the flash messages in a response.
Within development and test environments, Rails is usually configured to show a detailed debug page instead of 404s. However, there...
tl;dr: Upgrade the gem to at least 4.0.1 When you use rspec_rails in a version < 4 with Rails 6.1 you may encounter an error like this: Failure/Error:
...spec, so I first thought it could be the name of my Actor model. RSpec.describe Actor do it 'is valid with a name and birthday' do actor = Actor.new(name: 'Christian...
Though nowhere to be found in the official docs, this works just fine. describe Facebook::Post do it_behaves_like...
You will need to upgrade to RSpec >= 2 and rspec-rails >= 2 for Rails 3. Here are some hints to get started: In RSpec 2 the executable is rspec, not...
RSpec and rspec-rails have been completely refactored internally. All RSpec classes have been renamed from Spec::Something to RSpec::Something. This also means that every require 'spec/something' must...
RSpec's let allows you to super into "outside" definitions, in parent contexts. Example: describe '#save' do subject { described_class.new(attributes) } let(:attributes) { title: 'Example', user: create(:user) } it 'saves' do...
The Basic Authentication header encodes username and password. Effectively, it's just Base64 plus a "Basic" prefix.
...specs immediately or to get an overview of the core functionalities, you can use RSpec's "nested" format. It looks like this: Tool validations should require model to be set...
...should find tools by model and maker should find tools by serial number Call RSpec like this in order to use the nested format: spec spec -f documentation
Sometimes you need complex expectations on method arguments like this SomeApi.should_receive(:find).with(:query => '*foo*', :sort => 'timestamp ASC', :limit...