...k] + (f * (values_sorted[k + 1] - values_sorted[k])) end benchmarks = [ %(User.where('name ILIKE ?', '%test%').first), %(User.where('email ILIKE ?', '%test%').first), ] measurements = benchmarks.index_with do |_benchmark| [] end benchmarks.each do |benchmark...
View specs are a powerful tool to test several rendering paths by their cases instead of using a more costing feature spec. This is especially useful because they become quite...
...the view independent from the controller-logic. Therefore it will be more applicable to test views within request specs. But, even then, you still can use Caypbara's matchers within...
When you find similar groups of expect calls in your tests, you can improve readability by extracting the group into its own matcher. RSpec makes this easy by allowing matchers...
...to call other matchers. Example The following test checks that two variables foo and bar (1) have no lowercase characters and (2) end with an exclamation mark: expect(foo).to...
If a project ist configured to spawn CI runners for tests or deployment when pushing to the Repo, a habit of pushing WIP commits regularly may conflict with that.
...follow this workflow. Finishing a feature When you're done with your feature and tests are green, and your changes pass our merge request checklist: Within your feature branch, merge...
...trashed_changed? end Or, move the conditions into the callback. This also allows you test the conditions more easily using a unit test: after_save :update_offices_people_count
if office_id_changed? || trashed_changed? ... end end Also see the card on testing conditional validations...
The argument that using tests is a ideologic waster of time fails when one considers how it can help to insure architectural decisions...
I brought up the question whether tests should call the translation API when checking for the presence of a string...
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...
...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
...serve requests with the HOST header hacker.xyz You need to take care in your tests, that you reset ActionMailer::Base.default_url_options after e.g. request specs, e.g. spec/support/action_mailer.rb: RSpec.configure do...
...verification") added without_partial_double_verification. This might be handy in case you are testing helpers in Rails, where you sometimes rely on methods defined in the application controller (e.g...
...module UsersHelper def user_display_name [current_user.first_name, current_user.last_name].join(' ') end end Unit test with error describe UsersHelper do describe '#user_display_name' do it 'returns the first and...
...instead reference another part of that email by a content ID cid:xxxxx@xxxxx.mail. Your tests will not be able to verify easily that you used the correct image version any...
...last point, it is recommended to use a feature flag and disable inlining in tests by default so you will still be able to easily test that your emails use...
Your development machine is usually on a very good network connection. To test how your application behaves on a slow network (e.g. mobile), you can simulate limited bandwidth. Chrome
SMTP validation: connects to the host received from DNS and starts a test delivery to find out if the recipient mailbox actually exists tl;dr: We suggest you...
...change this attribute to hold a list of URLs, and need to adjust all tests. Our goal is to convert the url "..." to the urls ["..."] here. Open find and replace...
...and expected constants are resolved: class_name = params[:type].presence_in(%w[User Post Test]) if class_name class_name.safe_constantize.new # either User, Post or Test else Rails.logger.error "This should not happen...
...geordi png-optimize Carrierwave Go through our Carrierwave checklist if used. Maintenance Integrate and test the capistrano maintenance task. Accessibility Decide what effort you want to put in improving accessibility...
...Our first implementation might look like this: def group(content) " #{content} " end We run tests and realize that our helper escapes too much. Content appears like this:
baz: "example" We can now load it as usual, but pass freeze: true. >> test = YAML.safe_load_file('example.yml', freeze: true) => {"message"=>["hello", "universe"], "foo"=>{"bar"=>{"baz"=>"example"}}}
...itself is frozen: >> test.frozen? => true And no matter how deep you dig, everything inside that Hash is also frozen. >> test['message'].frozen? => true >> test['foo']['bar']['baz'].frozen? => true
...single feature/spec: Just paste the reference into your terminal (you need to "unmark" the test directories "as test sources root" to get a path relative to the project root).
Sometimes you just want to have a small web server that serves files to test something. Serve the current directory On Ruby 1.9.2+ you can do the following ("." for current...
...updating the corresponding mailer preview can be forgotten very easily. Mailer previews can be tested like other code as well and I sometimes add the following tests to test suites...
In this article, we’ll look at what side effects are, why they are problematic despite being necessary, and how...