Slow test suites are a major pain point in projects, often due to RSpec and FactoryBot. Although minitest and fixtures are sometimes viewed as outdated, they can greatly improve test...
...why is this setup faster? Partially, it's because minitest is more lightweight than RSpec, which as a testing DSL relies on Ruby's metaprogramming. The main speed boost, however...
...of the coverage into every executed spec: require 'testsort' coverage_measurement = Testsort::CoverageMeasurement.new coverage_measurement.start RSpec.configure do |config| config.after do |example| coverage_measurement.cover(example) end config.after(:suite) do coverage_measurement.finish end end
...and the saved coverage data, prioritize the specs by that and pass them to rspec to run in that order. Optional: You can also use the --strategy parameter to choose...
...are during run-time, it's definitely possible. It's a lot easier in RSpec 2+. For example, consider this global before block where you'd want to run some...
...code for specific specs only: config.before do # stuff that_fancy_method # more stuff end RSpec 2+ If you want to run such a block for a specific type of specs...
...in your E2E tests, and you won't need avatars for all of them: RSpec RSpec has a helper method file_fixture: describe User do let(:user) { build(:user, attachment...
...file_fixture('avatar.jpg').open) } end You can configure the directory where RSpec looks for fixture files: RSpec.configure do |config| config.file_fixture_path = "spec/custom_directory" end Alternatives: Rails.root.join('spec/fixtures/files/avatar.jpg').open('r') Rails.root.join...
...p and following the guidelines from the referred card above. If you are debugging rspec tests, you can use rspec -b to print the full stack trace. If you encounter...
...local Rails server's log ..or tail -f log/test.log to see the same for RSpec tests! Different debugging libraries Ruby has always had multiple gems for debugging. When you're...
...testing e.g. that a file user_export.xlsx is written to disk (smoke test) end end RSpec setup RSpec setup that avoids rewriting your scripts in a special way to make them...
stub_const('ARGV', args.map!(&:to_s)) load(script_path, true) end end RSpec.configure do |config| config.define_derived_metadata(file_path: Regexp.new('/spec/script')) do |metadata| metadata[:type] = :script
...run specs in the exactly same order like in the CI. Example for running rspec 3/8 with a seed output of 20689: CI_NODE_INDEX=2 CI_NODE_TOTAL...
...bundle exec rake "knapsack:rspec[--seed=20689]" Note: the environment variable CI_NODE_INDEX starts at 0 As an alternative you can copy and paste the test files from both...
...horizon').where('year > 1995').order(:year).to_a Adapt both the integration test and RSpec examples for the search functionality, using the guidelines from the "Testing" chapter in Growing Rails...
...Applications in Practice. RSpec examples should cover all edge cases E2E tests should only cover one "happy path" to show successful integration Open budgets in Project Hero (makandra only)
...und Nachteile von integration tests? Wann bringt man was zum Einsatz? Lerne Tests mit RSpec zu schreiben Verstehe welche Nachteile entstehen wenn man zu viel testet Tests schreiben kostet Zeit...
...sind weitere Tests nicht mehr so nützlich (teilw. Überschneidung von Tests) Lerne wie du RSpec einsetzt um deine Scripts zu testen Inhalte Writing basic tests with Rspec Rspec Dokumentation
As we are slowly switching from Cucumber scenarios to RSpec feature specs, you might be tempted to write assertions like this one: feature 'authorization for cards management' do
...behavior is that the Capybara test server is running in another thread, and the RSpec thread can't "see" the exception at this point in time. As we are emulating...
...or change the encoding. This is all possible, look up for the proper options. RSpec We used this helper in some project to do not have to write comma separated...
file end def generate_empty_csv file = Tempfile.new file.write(' ') file.rewind file end end RSpec.configure do |c| c.include CsvHelpers end The you can generate a csv file like this:
...enable the polling feature for the one E2E test that tests polling. In an RSpec request spec this would look like this: scenario 'The project list is updated periodically' do...
...know that you did not break anything. Prefer "big picture" integration-like tests (using RSpec) over unit tests. Acceptance and manual testing As early as possible, take actual time to...
...exceptions (previous default behavior, value false) Using the new default needs to change a RSpec test like this: Before it 'raises an exception if the page could not be found...
...need to check the underlying exception, you can temporary change the show_exception setting: RSpec.configure do |config| config.around(:each, :show_exceptions) do |example| show_exceptions = Rails.application.env_config['action_dispatch.show_exceptions'] Rails.application.env...
...to grab both the key and value from a hash Rerun Only Failures With RSpec RSpec can be configured to run only failed specs: RSpec.configure do |config| config.example_status_persistence...
...file_path = "tmp/failed_specs.txt" end # Then, run "rspec --only-failures" Disassemble Some Codes RubyVM::InstructionSequence#compile can be used to compile Ruby code on the fly. It offers a #disasm method...
...install the socat package) Start you integration test e.g. REMOTE_DEBUGGING=1 bundle exec rspec spec/system/some_spec.rb with a debugger set Open chrome://inspect/. It should list the test Chrome under...
...groups, are only excluded, when both of the groups are excluded, e.g. to exclude rspec in the following example, you will have to bundle without test and cucumber:
gem 'rspec'
...don't run the super method inside #initialize, the object is marked as frozen. Rspec mocks (e.g. using partial doubles with receive(partial_double).to ...) in tests would raise...
...the following error (just imagine how hard debugging can be): ArgumentError: Cannot proxy frozen objects, rspec-mocks relies on proxies for method stubbing and expectations. If you define initialize with...
find('.test2', visible: :hidden) finds the .test2 element As an example, an RSpec test which tries to confirm an element exists but is currently not visible, would say...
...just say create(:movie) instead of FactoryBot.create(:movie). You can configure this like so: RSpec.configure do |config| config.include FactoryBot::Syntax::Methods end Also see RSpec: Where to put custom matchers...
...file uploads with Rails Carrierwave: How to attach files in tests Carrierwave: Built-in RSpec matchers CarrierWave: Processing images with libvips Multipart formposts Content Disposition Header Exercise In MovieDB, allow...
Rendering of image versions and validation of file extension can be tested with RSpec These can be model specs for either the Movie or your PosterUploader
...screenshot can automatically save screenshots and the HTML for failed Capybara tests in Cucumber, RSpec or Minitest. Requires Capybara-Webkit, Selenium or poltergeist for making screenshots. Screenshots are saved into...
...specs to pass. This way we see Jasmine failures in our regular test runs. RSpec In a feature spec you can run Jasmine specs like this: scenario 'Jasmine tests' do...
...tests, that you reset ActionMailer::Base.default_url_options after e.g. request specs, e.g. spec/support/action_mailer.rb: RSpec.configure do |config| config.around(type: :request) do |example| url_options = ActionMailer::Base.default_url_options.dup example.run ActionMailer::Base.default_url...