Test that a form field has an error with Cucumber and Capybara
You can use the step definition below to say this:
Then the "Last name" field should have an error
Capybara
Then /^the "([^\"]*)" field should( not)? have an error$/ do |field, negate|
expectation = negate ? :should_not : :should
page.send(expectation, have_css('.field_with_errors', :text => field))
end
JustinFrench.com: Learn how to test your code
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, then write the code required to pass the test.
Test the status code of a response in Cucumber
Webrat
Then /^I should get a response with status (\d+)$/ do |status|
response.status.should include(status)
end
Capybara
Then /^I should get a response with status (\d+)$/ do |status|
page.status_code.should include(status.to_i)
end
Delay your Jasmine tests until the document is ready
To delay your entire Jasmine test suite until the DOM is ready, add the following:
beforeAll(function(done) {
$(done);
});
Check if an object is an ActiveRecord scope
Don't say is_a?(ActiveRecord::NamedScope::Scope)
because that is no longer true in Rails 3 and also doesn't match unscoped ActiveRecord classes themselves (which we consider scopes for all practical purposes).
A good way is to say this instead:
object.respond_to?(:scoped)
Mocking time in Jasmine specs
The easiest way to freeze or travel through time in a Jasmine spec is to use the built-in jasmine.clock()
.
- After
jasmine.clock().install()
you can use it to controlsetTimeout
andsetInterval
. - Using
jasmine.clock().mockDate()
you can mocknew Date()
(which returns the current time in Javascript)
While you can use SinonJS Fake timers, using the built-in Jasmine clock will save you an extra dependency.
Test xpath expressions in your browser
Safari & Chrome
Use $x()
in your console:
$x('//span') # selects all span elements
Firefox
There's an add-on.
Spec correct routing of custom URLs
When you roll custom URLs with hacks like routing-filter, you can put a spec like this into spec/routing/routing_spec.rb
:
Tearing Down Capybara Tests of AJAX Pages
An all-in-approach to fix the problem of pending AJAX requests dying in the browser when the server ends a test or switches scenarios.
We were able to work around this issue in most projects by doing this instead:
After '@javascript' do
step 'I wait for the page to load'
end
Cucumber: Removing uploaded files after test runs
Cucumber will clean up files you've uploaded in your Cucumber features automatically with the attached code. Put the file in features/support/
.
has_many :bugs, :through => :rails: Make your shoulda tests faster with fast_context
decided to go fixtureless with Shoulda + Factory Girl. All good, except one problem. Slow as fuck tests. So here’s fast_context as a solution for it. fast_context compiles all the ‘should’s within a context into a single test.
babushka: test-driven sysadmin
The idea is this: you take a job that you'd rather not do manually, and describe it to babushka using its DSL. The way it works, babushka not only knows how to accomplish each part of the job, it also knows how to check if each part is already done. You're teaching babushka to achieve an end goal with whatever runtime conditions you throw at it, not just to perform the task that would get you there from the very start.
Webservice to test and inspect requests
Requestb.in is a webservice that gives you a temporary URL you can use to test request. The page will automatically record and display the latest web request made to it.
CSS tests and experiments
The pages listed here contain tests and experiments about features, possibilities, browsers’ bugs concerning CSS.
That is, over 200 experiments.
How to test whether your device has a true Retina display
The linked site hosts a simple test. It shows two images with narrow vertical/horizontal lines. The more they resemble each other, the more Retina your display is.
Protip: Always leave a failing test
Mornings can be rough. To make them a little easier, leave yourself a failing test if your work isn’t finished. When you come back to the project, it makes it much easier to understand where you were and figure out what you still need to do.
Reflective Surface » Tests: Pragmatism or ideology?
The argument that using tests is a ideologic waster of time fails when one considers how it can help to insure architectural decisions.
Should I test for translated strings?
I brought up the question whether tests should call the translation API when checking for the presence of a string.
Alex Miller - Unit tests are a drop cloth
Unit tests are to refactoring like a drop cloth is to painting. Both feel like more work at first but ultimately save you time by allowing you to move faster.
How to test a confirm dialog with Cucumber? - Stack Overflow
Seems like there's no way to do it in Capybara, unfortunately. But if you're running your tests with the Selenium driver (and probably other drivers that support JavaScript), you can hack it
Speed up large Cucumber test suites
Test suites usually grow over time as more and more development time is spent on a projects. Overall run-time and performance of Cucumber suites in turn increases, too.
You can use the very same way Henning suggested for speeding up RSpec some time ago.
Put the following into features/support/deferred_garbage_collection.rb
Before do
DeferredGarbageCollection.start
end
After do
DeferredGarbageCollection.reconsider
end
We...
Continuous Security Testing with Devops - OWASP EU 2014
Interesting talk about a team that integrated automated security testing into their BDD workflow.
There is also a video of the talk.
Updated: Test a gem in multiple versions of Rails
Updated the card with our current best practice (shared app code and specs via symlinks).