...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...
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...
# features/support/env.rb require 'features/support/database_cleaner' require 'cucumber/rails/active_record' require 'cucumber/rails/world' Cucumber::Rails::World.use_transactional_fixtures = false RSpec # spec/support/database_cleaner.rb RSpec.configure do |config| config.before(:suite) do DatabaseCleaner.strategy = :transaction DatabaseCleaner.clean_with(:deletion) end config.around do...
DatabaseCleaner.start example.run DatabaseCleaner.clean end end # spec/spec_helper.rb RSpec.configure do |config| config.use_transactional_fixtures = false end Important Don't use a combination of config.before and config.after here otherwise around hooks are...
To check which elements an ActiveRecord relation contains use the contain_exactly matcher. describe User do let!(:admin) { create(:user...
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...
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...
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...
Though nowhere to be found in the official docs, this works just fine. describe Facebook::Post do it_behaves_like...
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...
You can define methods in any example group using Ruby's def keyword or define_method method. These helper methods...
@user = User.make end # ... end Furthermore, you will probably run into trouble with your RSpec configuration block that does things in config.before(:example), as this will also be run after...
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...
By default, Cucumber uses mocha. This note shows to use RSpec stubs and mocks instead. Rspec 1 / Rails 2 Put the following into your env.rb: require 'spec/stubs/cucumber' Rspec 2 / Rails...
RSpec 1 (Rails 2) With the most recent spec_candy.rb helpers you can say: User.stub_any_instance(:foo => :bar) user = User.new user.foo # => :bar RSpec 2 (Rails 3) RSpec 2 comes with...
...this feature built in: User.any_instance.stub(:foo => :bar) user = User.new user.foo # => :bar RSpec 3 RSpec Mocks 3.3: any_instance allow_any_instance_of(User).to receive(:foo).and_return(:bar)
...parts of your application are not tested. Integrating this in a rails project with rspec, cucumber and parallel_tests is easy. Add it to your Gemfile and bundle
...NUMBER part if you don't use parallel_tests # ... Require it in spec/spec_helper.rb for rspec (important: require it before anything else, especially rails!) require 'simplecov' SimpleCov.command_name 'specs' + (ENV['TEST...
RSpec 1, RSpec 2 To test whether two arrays have the same elements regardless of order, RSpec 1 and 2 give you the =~ matcher: actual_array.should =~ expected_array Rspec 3
...RSpec 3's expect syntax you can choose one of these two matchers: expect(actual_array).to match_array(['1', '2', '3']) expect(actual_array).to contain_exactly...
If you’re testing the behavior of deprecated code in your Ruby project, the warning messages littered throughout your spec...
...speed up your tests. I found that deferring garbage collection would speed up my RSpec examples by about 15%, but it probably depends on the nature of your tests. I...
...Test::Unit in his example, here is how to apply the same trick to RSpec. First, copy this class to spec/support/deferred_garbage_collection.rb: class DeferredGarbageCollection DEFERRED_GC_THRESHOLD = (ENV['DEFER_GC...
Shared example groups are a useful RSpec feature. Unfortunately the default directory structure generated by rspec-rails has no obvious place to put them. I recommend storing them like this...
...specs, put the following into your spec_helper.rb (for rails 4 in rails_helper.rb), above the RSpec.configure block: Dir[Rails.root.join("spec/models/shared_examples/**/*.rb")].each {|f| require f} Dir[Rails.root.join("spec/controllers/shared_examples/**/*.rb")].each {|f...