9 posts about #testing
Behavior of Capybara's "have_no_content" when expected with `.to_not` and with `.to`
about to_not have_content
:
expect(page).to_not have_content("not on page")
it waits (Capybara's default waiting time), then it pass. Don't use it! 🚫
whereas to have_no_content
:
expect(page).to have_no_content("not on page")
Waits (same as with .to_not
) but it passes as soon as "not on page"
disappears from the page; use this one to wait for loading messages to disappear! ✅
How to run a single test from a file using Cypress
I can run all tests from a file with:
yarn run cypress:run -s path/to/file.spec.js
but what about when I want to run only one test case from that file and not all of them?
We could use only()
// path/to/file.spec.js
it.only('just run this test', () => { ... })
it('not run this test', () => { ... })
// we could also use describe.only() but IDK if that is a bug or a feature xD
Run again yarn run cypress:run -s path/to/file.spec.js
to see how the it.only()
test will run it
while on a Capybara / Cucumber test execution How to save and open the page as snapshot in a browser for inspection
While debugging a cucumber test (within an specific step for an specific scenario) with a binding.pry I wanted to see the html as it is with inputs and outputs of data at the moment (and because I usually run tests with Capybara headless option), I realized the existance of:
save_and_open_page
it Saves a snapshot of the page and open it in a browser for inspection
https://www.rubydoc.info/github/jnicklas/capybara/Capybara%2FSession:save_and_open_page
Note: ^ be sure to have the gem 'launchy' within test env so that it works
Test Rails log messages with RSpec
Have you wondered how to test your Rails log messages?
Try this:
it "logs a message" do
allow(Rails.logger).to receive(:info)
expect(Rails.logger).to receive(:info).with("Someone read this post!")
visit root_path
expect(page).to have_content "Welcome to TIL"
end
Or this:
it "logs a message" do
allow(Rails.logger).to receive(:info)
visit root_path
expect(page).to have_content "Welcome to TIL"
expect(Rails.logger).to have_received(:info).with("Someone read this post!")
end
Stubbing a block in rspec
There're at least two ways to stub a block in rspec, the first version is using and_yield
config = double('Config', enabled: true)
allow(app).to receive(:config).and_yield(config)
app.config do |config|
expect(config.enabled).to be_truthy
end
The second version is receiving the block
config = double('Config', enabled: true)
allow(app).to receive(:config) do |_, &block|
block.call(config)
end|
app.config do |config|
expect(config.enabled).to be_truthy
end
Approval Tests are a thing
In normal unit testing, you say expect(person.calculate_bmi).to eq(21)
or something like that. Approvals allow you to assert the state of complex objects against a known state.
For example, you can say, Approvals.verify(person)
I had not heard of this approach, which is posed as an alternative to traditional TDD. http://approvaltests.com
Debug Network Activity in Google Chrome
Today I learned about the existence of Google Chrome Net Internals, a utility that gives you extensive abilities related to debugging network activity in your browser. Used it as part of trying to diagnose why some of my Rails project links were hanging in development mode.
Why Capybara requires button for submitting forms
Despite being a super common request, Capybara's API doesn't give you a way to submit forms directly (without hitting a submit button). The denial to do so is actually a principled stance, as you can read for yourself in this pull request. In a nutshell, Jonas believes its a bad practice to do so, plus there is no standard way to support the functionality across all browsers.
Workarounds exist, but seem clunky.