...should extract that behavior into a trait or module. This card describes how to test that extracted behavior without repeating yourself. Note that the examples below use Modularity traits to...
...app/models/page.rb class Page < ApplicationRecord include DoesSanitizeHtml end # app/models/template.rb class Template < ApplicationRecord include DoesSanitizeHtml end Testing test trait usage with a shared example group When two classes share behavior through a...
...return means "exit the lambda" How to define a lambda With the lambda keyword test = lambda do |arg| puts arg end With the lambda literal -> (since Ruby 1.9.1)
...also are next and continue How to define a block With the proc keyword: test = proc do |arg| puts arg end With Proc.new: test = Proc.new do |arg| puts arg
...text-transform: uppercase - especially on form labels - can cause you serious headaches in Selenium tests. Sometimes the web driver will see the uppercase text, sometimes it won't, and umlauts...
...will be a problem as well. Simply disable it in tests, by adding a body class for tests %body{'data-environment' => Rails.env} overriding the transforms [data-environment="test"] * text-transform...
...present. Otherwise, the browser uses its local time. This can lead to issues in tests with mocked time or inconsistent cache behavior. Cookie Expires depends on the Date header or...
...lead to unexpected issues in specific scenarios. Why does this matter? Mocked time in tests Consider a Rails application where you use the remember_me feature of Clearance for authentication...
Sometimes you have a test expectation but actually want a better error message in case of a failure. Here is how to do that. Background Consider this test: expect(User.last...
expect(User.last).to be_present, 'Could not find a user!' Now your test will fail with an actually helpful error message: Could not find a user! (Spec::Expectations...
These steps are now part of Spreewald. The step definitions below allow you to test the filename suggested by the server: When I follow "Export as ZIP"
Prefer request specs over end-to-end tests (Capybara) to joyfully test file downloads! Why? Testing file downloads via Capybara is not easy and results in slow and...
...fragile tests. We tried different approaches and the best one is just okay. Tests for file downloads via Capybara ... ... are slow, ... are fragile (breaks CI, breaks if Selenium driver changes...
...pack is application.js): = image_pack_tag('media/application/images/logo.png') Deployment Follow Configuring Webpacker deployments with Capistrano. Tests In your cucumber test, you will want to regenerate your assets before each test suite...
...run. Since this is a bit slow, especially when using multiple processes with parallel_test, read this card. jQuery If you still require jQuery, add it to your webpacker project...
No one wants to cry over regression issues in views; does testing HTML and CSS have to be such a back and forth between designers and devs? Why is it...
...presentation layer? Well, GreenOnion is here to help you get the same results on testing front-end styling that you've enjoyed in your unit and integration tests up to...
1) first expectation failed 2) second expectation failed As you can see, the test is not being interrupted by the first two falsy expectations, the third expectation passed without...
This can be handy if you are testing for multiple values, which map to one context, e. g. mail components, in order to provide better readability of your...
...implement sending an error notification if the data (in the db) is too old. Testing: If you use sidekiq_retries_exhausted (unit test): https://stackoverflow.com/questions/33930199/rspec-sidekiq-how-to-test-within-sidekiq-retries-exhausted-block-with-another If you use the...
...global option (manual test): # Use this worker to test if the exception notifier for sidekiq works as expected and the retries total duration fits. # Example: TestWorker.perform_async('foo', 'bar')
...you need additional form fields (like tax IDs) to bill customers in another language. Tests Tests should continue to use the base language you had before starting localization. Don't...
...rewrite all tests and don't test every screens with every language unless repeatedly broken translations becomes a pain point. Do test non-trivial language-specific customizations like the parsing...
...integration of Pivotal's excellent license_finder that validates all dependencies during a regular test run. It will protect you from accidentally adding libraries with problematic licenses. Installation
LicenseFinder can exclude certain dependency groups from its report, for example development and test (add devDependencies for yarn). This only works with certain package managers (among which Bundler and...
...Add continuous integration in Gitlab 4 0 - 5 Dev machines are not blocked on test runs; Code Reviews include test badge; Automatically merge a PR on green tests
...library has a better API Add capybara_lockstep 2 0 - 5 Reduce flaky integration tests
Understand why we test: Low defect rate without a QA department. Customer acceptance testing can concentrate on new features and things a robot cannot do (e.g. how does a...
...without needing to understand the entire system. Why do we use different types of tests? What are the pros and cons of unit tests? What are the pros and cons...
...that are built into RSpec. Play with some of these matchers in your MovieDB tests. The benefits of using better matchers Which of the following two lines is better? Why...
...in your MovieDB. It saves a duplicate of the movie, copying all the attributes. Test this method using your new have_same_attributes_as matcher. Tip ActiveSupport gives your arrays...
...or an argument. return # => LocalJumpError: unexpected return example { return 4 } # => LocalJumpError: unexpected return def test example { return 4 } return 'test' end test # => 4 Note how test returns the return value...
...from the block; neither code after the example invocation (returning "test") nor code after the yield inside example (putsing "done", returning "example") are executed. As you always knew, and in...
end end def bar 'New bar' end end module SuperClient prepend SuperClientExtension end Test class Test; include SuperClient; end Test.foo => 'New foo' Test.new.bar => 'New bar' Good practice
Tests are about 100% control over UI interaction and your test scenario. Randomness makes writing tests hard. You will also push tests that are green for you today, but red...
This creates ugly sample data like "First3 Last3", but e.g. doesn't make tests pass when they shouldn't...
...last_name = last_name end attr_reader :first_name, :last_name end To just test if an argument is a Person, you have several options: Test that the argument is...
...instance of a certain class expect(object).to receive(:foo).with(instance_of(Person)) Test that the argument responds to certain methods expect(object).to receive(:foo).with(duck_type...
Jasmine is a great tool to unit test your JavaScript components without writing an expensive end-to-end test for every little thing. Jasmine is a purely client-side tool...
...or access the database. Any HTTP calls must be mocked. Learn What do we test with Jasmine? Just as unit tests are a direct way to closely look at your...
When testing JavaScript functionality in Selenium (E2E), you may need to access a class or function inside of a evaluate_script block in one of your steps. Capybara may only...
...are exporting your definition(s) in Webpacker, these won't be available in your tests (and neither in the dev console). The following principles/concepts also apply to Sprockets.
Haml HTML generated by Haml 5 HTML generated by Haml 6 %button(up-test) %button(up-test=true) %button(up-test=false) %button(up-test=nil) %button(up-test...
same %button(up-test='false') same %button(up-test='') same Extending the list of attributes considered "boolean" You can add to the list of "boolean attributes", but please do...
...can use this command: :%s/{{\([^\}]*\)}}/%{\1}/cg Fix deprecation warnings with :overwrite_params Run tests Deploy See the commit log for a detailed list of changes. 2.3.9 to 2.3.10
...Upgrade rails gem Change your environment.rb so it says RAILS_GEM_VERSION = '2.3.10' Run tests Deploy Also see commit log. From 2.3.10 to 2.3.11 Fixes multiple security issues.