...and_call_original object.should_receive(:some_method).with('my argument').and_return('other value') Requires rspec-mocks...
RSpec let's you chain a matcher with .or. The expectation will then pass if at least one matcher matches: expect(color).to eq("red").or eq("green")
...of awesome shell scripts, has been extended by three scripts to help you call RSpec or Cucumber: cuc This script runs Cucumber the way you want it: Prints some line...
...t wip features/users.feature # run one feature with parameters like cucumber rs This script runs RSpec the way you want it: Prints some line feeds to easily find your test results...
When you write a custom RSpec matcher a good place to store them is to create one file per matcher in spec/support/matchers: spec/support/matchers/be_same_numbers_as.rb spec/support/matchers/be_same_second_as.rb spec/support/matchers/exist_in_database.rb spec/support/matchers/include_hash.rb You can include all...
Hint: There's another card with this helper for Cucumber features. Sometimes you feel like you need to stub some...
...For this purpose the gem allows you to record execution time when you call rspec with --format ParallelTests::RSpec::RuntimeLogger --out tmp/parallel_runtime_rspec.log. As the documentation on this states you can...
...make this the default option by adding the following to your .rspec (or .rspec_parallel) file: --format progress --format ParallelTests::RSpec::RuntimeLogger --out tmp/parallel_runtime_rspec.log Knapsack For knapsack to generate reports...
This raises "Could not find first Keyword": describe Keyword do it { should validate_uniqueness_of(:text) } end Do this instead...
...bash aliases helped me speed up that process: alias show-next-failure="bundle exec rspec --next-failure" alias open-next-failure="show-next-failure || show-next-failure --format json | jq...
...Define an alias to run the next failure alias show-next-failure="bundle exec rspec --next-failure" # IFF there is a failure, print it to the console, then
...test to ensure it works within your app. The test should fail: bundle exec rspec spec/requests/default_etag_spec.rb Fixing random CSP nonces Open config/initializers/content_security_policy.rb and change the content_security_policy_nonce_generator...
Checking successful integration The test we added earlier should now pass: bundle exec rspec spec/requests/default_etag_spec.rb To manually test ETags, open the Network tab of your DevTools. Then load the...
...have automated tests for all Ruby/Rails combinations as well. Tests usually live in spec/ (RSpec) or test/ (used by multiple test frameworks) or features/ (Cucumber). Most gems only need unit...
...tests (as they're code libraries, not applications) which we mostly write in RSpec. The test setups in our gems have evolved over time, and are a bit scattered at...
...your specs, please make sure that you have not introduced a flaky spec. Using RSpec matchers One rule of thumb I try to follow in capybara tests is using capybara...
...matchers and not plain rspec matchers. One example: visit(some_page) text_field = find('.textfield') expect(text_field['value']).to match /pattern/ This can work, but is too brittle and...
...Copy the file to a new location that is not tracked by Git Run RSpec example by description Run RSpec example by nesting index
...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...
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...
How to configure # in ~/.gitattributes: *.rb diff=ruby *.rake diff=ruby *_spec.rb diff=rspec *.feature diff=cucumber Then tell Git where it can find its global attributes file with...
...this on a per-project base as well.) Ruby support is built in, but RSpec and Cucumber need a little more help: # in ~/.gitconfig [diff "rspec"] xfuncname = "^[ \t]*((RSpec|describe...
...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...
.../public/assets/', '/public/assets-test/', '/tmp/', ], 2. Report all existing offenses and exclude them initially Add an RSpec test, which runs ESLint (also during a full RSpec test run), e.g. in spec/eslint_spec.rb:
RSpec.describe 'eslint' do it 'is not offended' do stdout, stderr, status = Open3.capture3('yarn', 'run', 'eslint', '.') failure_message = [stderr, stdout].reject(&:empty?).join("\n\n") expect(status.success?).to eq(true...
...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
...runners like in the following example .gitlab-ci.yml: rubocop: interruptible: true script: - 'bundle exec rubocop' rspec: interruptible: true script: - 'bundle exec rspec' Afterwards new pushes to your MR will cancel all...
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...
...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...
...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...