Capybara: Running tests with headless Chrome

Posted Over 3 years ago by Henning Koch.

...Looking at the hidden browser Option 1: Run without headless You can call your tests with NO_HEADLESS=1 bundle exec cucumber to see the Chrome window for this test...

...network targets and add localhost:9222 in the Target discovery settings Start you integration test e.g. REMOTE_DEBUGGING=true bundle exec rspec spec/system/some_spec.rb with a debugger set You should now...

Testing ActiveRecord callbacks with RSpec

Posted About 8 years ago by Henning Koch.

Our preferred way of testing ActiveRecord is to simply create/update/destroy the record and then check if the expected behavior has happened. We used to bend over backwards to avoid touching...

Today we would rather make a few database queries than have a fragile test full of stubs. Example Let's say your User model creates a first Project on...

How to access Chrome Devtools when running JavaScript tests via CLI

Posted Over 2 years ago by Michael Leimstädtner.

While we are used to run our JavaScript tests on a test page within our Browser, it's also possible to run them on the command line with NodeJS. I...

...think that's actually the most common way to run JS tests. Given a Vue project that uses Jest (via vue-cli-service) with the following package.json: { "scripts": { "test": "vue...

JavaScript: Testing the type of a value

Posted Over 6 years ago by Henning Koch.

...or when working on a project without LoDash. Booleans This one is straightforward to test with typeof: x = true typeof x === 'boolean' // => true Strings Strings can exist as literal ("foo...

...we cannot rely on typeof: typeof "foo" // => "string" typeof new String("foo") // => "object" Your test should check both forms: x = 'foo' typeof x === 'string' || x instanceof String // => true Numbers

How to test Autoprefixer and CSSnext in PostCSS

Posted About 5 years ago by Emanuel.

...afterwards again. CSSnext Input: body { font-variant: small-caps; background-image: image-set(url(test_1.svg) 1x, url(test_2.svg) 2x); } Output: body { -webkit-font-feature-settings: "c2sc"; font-feature-settings: "c2sc...

...font-variant: small-caps; background-image: -webkit-image-set(url(test_1.svg) 1x, url((test_2.svg) 2x); background-image: image-set(url(test_1.svg) 1x, url(test_2.svg); } Autoprefixer Input: body { ::placeholder { color: gray...

Use DatabaseCleaner with multiple test databases

Posted About 2 years ago by Dominic Beger.

You may have asked yourself how you're able to keep your test databases clean, if you're running multiple databases with full read and write access at...

development: primary: <<: *default database: my_app_development migration: <<: *default database: my_app_migration test: &test primary: <<: *default database: my_app_test_<%= ENV['TEST_ENV_NUMBER'] %> pool: 20 migration: <<: *default...

Rails < 5: How to get after_commit callbacks fired in tests

Posted Almost 6 years ago by Emanuel.

...cleaner gem with strategy :transaction, after_commit callbacks will not be fired in your tests. Rails 5+ Rails 5 has a fix for this issue and no further action is...

Rails 3, Rails 4 Add the gem test_after_commit to your test group in the Gemfile and you are done. You don't need to change the database...

Disable PostgreSQL's Write-Ahead Log to speed up tests

Posted 5 months ago by Michael Leimstädtner.
edgeapi.rubyonrails.org

The linked article suggests an interesting way to speed up tests of Rails + Postgres apps: PostgreSQL allows the creation of “unlogged” tables, which do not record data in the PostgreSQL...

...production environments. If you would like all created tables to be unlogged in the test environment you can add the following to your test.rb file: # config/environments/test.rb ActiveSupport.on_load(:active_record...

How to employ and run your tests with parallel_tests to speed up test execution

Posted Almost 13 years ago by Ulrich Berkmueller.

When your cucumber features grow massively over time, the test execution can take a lot of time. One easy way to speed up your test execution is to use the...

...parallel_tests gem. It comes along with some useful rake tasks that let you setup your local test environment shortly to run your features, specs or unit-tests in parallel...

Jest: How to clear the cache

Posted Over 2 years ago by Michael Leimstädtner.

I recently was in a weird situation where my (Jest/CLI) tests were referencing a function that was no longer part of my code - I had just refactored it. Apparently Jest...

Capybara: Waiting for pending AJAX requests after a test

Posted About 9 years ago by Henning Koch.

When ending a Selenium test Capybara resets the browser state by closing the tab, clearing cookies, localStorage, etc. It may be a good idea to wait for all in-flight...

...If that JavaScript opens an error alert or spams errors to the console, your test may fail after the last step. With unlucky timing the server may receive an AJAX...

Debug flaky tests with an Unpoly observeDelay

Posted Over 1 year ago by Niklas Hasselmeyer.

...as well as their programmatic variants up.observe() and up.autosubmit() are a nightmare for integration tests. Tests are usually much faster than the configured up.form.config.observeDelay. Therefore, it may happen that you...

...waiting for the observeDelay to run out, there is no XHR request and the test will continue. The solutions Lower the observeDelay in tests This will always help you. Your...

Carrierwave: How to attach files in tests

Posted Over 2 years ago by Emanuel.

...is handled by Carrierwave uploaders (or maybe any other attachment solution for Rails) in tests allows different approaches. Here is a short summary of the most common methods.

...also be interested in this card if you see the following error in your test environment: CarrierWave::FormNotMultipart: You tried to assign a String or a Pathname to an uploader...

Capybara: Preventing server errors from failing your test

Posted About 3 years ago by Henning Koch.

When your Rails application server raises error, Capybara will fail your test when it clears the session after the last step. The effect is a test that passes all steps...

Generating test images on the fly via JavaScript or Ruby

Posted About 6 years ago by Arne Hartherz.
jsfiddle.net

When you need test images, instead of using services like lorempixel or placehold.it you may generate test images yourself. Here we build a simple SVG image and wrap it into...

Heads up: You should always use "current_window.resize_to" to resize the browser window in tests

Posted 3 months ago by Michael Leimstädtner.

I recently noticed a new kind of flaky tests on the slow free tier GitHub Action runners: Integration tests were running on smaller screen sizes than specified in the device...

...browser_to(MOBILE_WIDTH, MOBILE_HEIGHT) else # Nothing to do in case of rack tests end end end This could however reduce your test performance...

RSpec: how to prevent the Rails debug page if you want to actually test for 404s

Posted Over 2 years ago by Klaus Weidinger.

Within development and test environments, Rails is usually configured to show a detailed debug page instead of 404s. However, there might be some cases where you expect a 404 and...

...want to test for it. An example would be request-specs that check authorization rules. (If you use a gem like consul for managing authorization rules, you should always check...

Rails and Postgres: How to test if your index is used as expected

Posted Over 6 years ago by Emanuel.

...index in your migration and migrate add_index :users, [:last_name, :created_at] 3. Test the index in your database ActiveRecord::Base.connection.execute('SET enable_seqscan = OFF') # Try to force index...

Jasmine: Testing complex types for equality

Posted Almost 9 years ago by Henning Koch.

Jasmine comes with two matchers that test for equality. The first is toBe: expect(first).toBe(second) toBe passes when first === second. Unfortunately this is useless for non-primitive values...

...teach toEqual additional notions of equality. E.g. the following code will teach toEqual to test two jQuery collections for equality: beforeEach -> jasmine.addCustomEqualityTester (first, second) -> if first instanceof jQuery && second instanceof...

JustinFrench.com: Learn how to test your code

Posted About 14 years ago by Lexy.
justinfrench.com

Eventually you’ll forget that you used to spend hours testing your code in a browser, and start complaining that your automated tests are taking minutes to run! You’ll...

...have intense debates with co-workers about what to test and how to test it properly. You’ll start writing the test first to expose the bug or missing feature...

Run a single test in Test::Unit

Posted Over 13 years ago by Henning Koch.

To run a single test file: rake test:units TEST=test/unit/post_test.rb rake test:functionals TEST=test/functional/posts_controller_test.rb rake test:integration TEST=test/integration/admin_news_posts_test.rb You may even run a single test method:

...I test test/unit/post_test.rb -n "name of the test" ruby -I test test/functional/posts_controller_test.rb -n test_name_of_the_test # underscored, prefixed with 'test_' Or all tests matching a regular expression:

On Being A Journeyman Software Craftsman: Test-first and Test-driven conversation with JB Rainsberger

Posted Almost 15 years ago by Lexy.
programmingtour.blogspot.com

We had a conversation about the fact that the 'TDD is about testing vs TDD is about design" debate that keeps popping up, especially now in the Ruby community...

Jasmine: Mocking ESM imports

Posted Over 1 year ago by Henning Koch.

...you want to spy on a function that is imported by the code under test. This card explores various methods to achieve this. Example We are going to use the...

// client.js import { hello } from 'lib' function helloTwice() { hello() hello() } export helloTwice In our test we would like to show that helloTwice() calls hello() twice. We find out that it...

Rails: How to test the parsed response body

Posted About 1 year ago by Emanuel.

Testing your responses in Rails allows to parse the body depending on the response MIME type with parsed_body. get '/posts.json' response.parsed_body # => [{'id' => 42, 'title' => 'Title'}, ...]