Asserting a partial was rendered in an RSpec view spec

View specs are great. While I always keep in mind they are a unit test and do not test the full integration of a feature, I find that they are fantastic for asserting around all the edge cases of a feature - things like the expected validation messages showing up, or buttons appearing or disappearing based on different scenarios. I...

CLI tool entr for running a command when a file changes

entr is a tool I’ve been using a bunch recently - mostly for watching source files, and running tests when I change the source file. entr can be piped one or more files, which means it’s really easy to use - just use ls, find, or any other tool that outputs filenames, and then pipe it to entr: ls app/models/widget.rb...

Assert window.location properties with Jest and jsdom

I have a React component that is part of a wider Ruby on Rails application. It fetches a record, and if the record does not exist, it redirects to the server-rendered 404 page. Unfortunately, while the user will see the ‘not found’ page, the HTTP status code will be 200 OK by default, because this page is static. Best practise...

Mid King Biv Trip Report

This trip report is really more a cautionary tale of overconfidence and inexperience leading to a challenging Tararua overnight trip to Mid King Biv. Started: 11am from Holdsworth Carpark Finished: 6:30pm the next day at Holdsworth Carpark Travel Times: Day 1: 2 hours to Atiwhakatu Hut (break) 1 hour from Atiwhakatu Hut to the swingbridge over Atiwhakatu Stream 20 minutes...

Quickly access Ruby Mail object parts

Often, mailer specs will start with a simple assertion like expect(mail.body).to include "Some text in the expected email here". This works for a while, but quickly breaks down when an email grows more complex, such as having special characters causing it to be encoded as 7bit or QuotedPrintable, having attachments, or supporting different formats (like plaintext & HTML). I’ve always...

Access ActionView context in an RSpec test

I recently created a Presenter object to wrap around an ActiveStorage::Blob that I wanted to decorate with some presentation methods for things like a human file size and content type. The initialisation signature for my presenter looks like this: class MyPresenter < SimpleDelegator def initialize(obj, view_context) super(obj) @view_context = view_context end def human_file_size @view_context.number_to_human_size(byte_size, precision: 1) end # more presentation...

Access parent node with Capybara

Sometimes I’ll need to find an element in relation to another. An example of this is a link with a single icon element inside it: <a href="/example"><img src="icon.png" alt="Example Page" /></a> Because Capybara element finders can be chained (e.g. you can do find().find().find() to traverse down elements, the ability of XPath to traverse the DOM tree can be used to...

ActiveRecord::Base.update actually calls model setter methods

When an update method like @my_record.update(test_attr_1: "foo", test_attr_2: "bar") is called, my assumption was that those attributes immediately got passed to some ActiveRecord magic to update the record. Not true! Each attribute key calls the setter on the object - in this case, @my_record.test_attr_1=(new_value) and @my_record.test_attr_2=(new_value) would be called. This is neat because you can ‘redirect’ that attribute assignment to...

Handy Ruby shorthand for testing class ancestors

Today I was working on a method that had to take a string, and resolve it to a class. This method needed to perform some validation on the model class, to make sure it was a type we expected. My first iteration of this didn’t work - I expected that I could use Object#kind_of?, however this only works for testing...

Go offline with Capybara, Selenium and Chromedriver

There’s some preamble here. Jump to solution Recently, I’ve been working on a client project that needs to function offline. This means that I have a subset of tests where I need to change the connectivity status of the browser driving the tests to simulate being offline to make assertions that the application is behaving correctly. I already knew that...