Learn to create test data effectively using factories. Decouple tests by having each test start with an empty database and create only the records needed for the test.
...Factories, not fixtures By default Rails uses global fixtures for its tests. This is a giant world of example data that grows with every test. In our experience the use...
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...
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...
...and show a Google map centered around it Now write an E2E feature that tests that the map shows the correct location. Hints The purpose of this lesson to learn...
...should expose an API that lets you query the current map position from your test. Your test can use evaluate_script(...) to talk with that JavaScript. Note Adding leaflet to...
...created. When editing a movie there is a "Year" field that can be changed. Testing: Learn techniques Write tests for both exercises. Write multiple variants, each using a different approach...
...you minimize the number of lines of code that now no longer run during tests? Mock out the network request to the API using Webmock Mock out the network request...
We use Selenium WebDriver integrated with Cucumber/Capybara for full-stack integration testing. Try and use it Your forked MovieDB should already include a feature that uses a real browser. Add...
...the @javascript tag to your other features to test it yourself. When you run your cucumber feature now with NO_HEADLESS=1 geordi cucumber, you should see a browser opening...
...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 problems...
...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 working...
...number of words, lines and paragraphs, and outputs the result. For example: $ ruby count_words.rb test.txt test.txt has 123 words test.txt has 13 lines test.txt has 4 paragraphs Hint
...strings in locale files. This way you can override them with your own wording. Testing localization Localization is a cross-cutting concern that affects all screens.
...coverage you would need to duplicate all your tests for every single locale. This is not sustainable. We recommend the following instead: Keep most of your tests in the first...
...Common mistakes when storing 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...
...form is displayed again. Use the CarrierWave cache for this. Make sure to add tests for adding, changing and deleting a poster image: An integration test for the happy path...
...applications that are either old or abandoned by a different team earlier: Add E2E tests for the happy path. Other than unit tests, E2E tests will not break when we...
...refactor later. Always add tests on whatever we work on. When you work on something, improve that part of the code. Make sure setup for a new developer is as...
...poster as a file attachment to the e-mail? Can you write a RSpec test for this? In the lesson Form Models we said that we don't like to...
...method like :smtp or :sendmail which can be different for each environment. For the test environment the delivery method is set to :test, which means that new e-mails are...
Using a gem like rails_state_machine Make sure that you have tests for all the changes you make. Note that your E2E-Tests should rather focus on...
...should create records in a state that is useful for the vast majority of tests. Tip We often use state machines to track the state of "wizard forms", i.e. long...
...yourself in Cucumber scenarios. Cucumber Factory We have a very useful gem to create test data in cucumber: cucumber_factory. Read through the README. Integrate it into your MovieDB and...
...you cannot find any opportunities to DRY up a Cucumber scenario, do this exercise (testing the file upload from before): Write a scenario: User can upload a movie poster in...
...main').search('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...
...should be ignored and no validation errors should be shown. Make sure to write tests for the showtimes form. How do you refer to the showtimes fields when there are...
...must not contain any code or names specific to movies or the movie form. Test that the component is reusable by allowing to add an actor's roles from within...
...should have an overview which kinds of validations are built into Rails. Also read Testing ActiveRecord validations with RSpec. Make the following changes to your MovieDB: Make sure your MovieDB...
...for black and white movies and release_year >= 1917 for color movies. Add tests for your validation. Make sure you have a test for each case...
...only see their own user as an option in the same . Remember to add tests for your authorization code. Tip If you have existing dropdowns that accept a restrict list...
Discussion Discuss with your mentor: We don't want to duplicate our integration tests for every screen and user role. Why? Where to put authorization scenarios? In an authorization.feature...
...For each chapter: You should do all the programming exercises. You can skip the testing exercises. We are going to learn testing in a later card. We're also using...
...different testing frameworks and different types of tests than the tutorial. We will also be using a different development environment than the book suggests. Deviate from the following instructions:
...few modern features like "subgrid", ":has()" or "optional chaining" on Caniuse. Understand how we test for older browsers using BrowserStack Ask a colleague for our shared BrowserStack credentials
...keep maintaining the library if the original author stops doing so. Does it contain tests? The code quality of our libraries should generally be around the same level of our...
Use vector graphics Create multiple sizes of the image Use media queries that test for min-device-pixel-ratio Use Use Ignore the issue Discuss the pros and cons...
...kinds of work is done in those cronjobs. Can you find the code? The tests? What happens when an error occurs? How do we get notified? Understand that each call...
...of the release year is a good candidate. Make sure your code is (still) tested. Read the Sidekiq testing guide for some pointers. Sidekiq comes with a Web UI. Mount...