Ross-Hunter: railsPosts tagged with railshttp://www.ross-hunter.com2018-12-23T00:00:00ZRoss HunterDon't Test Like That!http://www.ross-hunter.com/2018/12/23/dont-test-like-that/2018-12-23T00:00:00Z2022-08-01T11:08:27-04:00Ross HunterAs a consultant, I come across a lot of bad test suites in Rails and JavaScript apps. The following are all RSpec, Jest, or Jasmine tests that I have found in the wild – with minor changes for clarity or to protect the guilty :) (myself included!)
These are some simple things not to do, along with some advice about what to do to accomplish the same goal.
Don’t test internals in a system test
SystemJSON API Helpers for API Testing in Rails using Airbornehttp://www.ross-hunter.com/2017/01/16/json-api-airborne-expectation-helpers/2017-01-16T00:00:00Z2022-08-01T11:08:27-04:00Ross HunterUpdate 05/08/2017
I made a gem! https://github.com/Ross-Hunter/jsonapi_expectations
I am a huge fan of the Airborne gem for testing APIs. Airborne uses rest_client to make HTTP requests and provides the following properties for use in your tests.
response - The HTTP response returned from the request
headers - A symbolized hash of the response headers returned by the request
body - The raw HTTP
Index Your Foreign Keyshttp://www.ross-hunter.com/2014/06/13/index-foreign-keys/2014-06-13T00:00:00Z2022-08-01T11:08:27-04:00Ross HunterWe add indexes to our database in order to speed up read operations. Basically, an index is a sorted copy of a particular set of columns the database uses to quickly look up the attributes of another table, most commonly, foreign keys of relations. There are several ways this can actually be implemented on the database; essentially, we tell the database which values we’ll be looking up most often andActiveRecord Associations & Youhttp://www.ross-hunter.com/2014/06/12/activerecord-associations/2014-06-12T00:00:00Z2022-08-01T11:08:27-04:00Ross HunterI love abstraction. It’s great to compartmentalize complexity and keep your application modular. SQL is not that. Relying on SQL strings in your code ties your implementation directly to the type, structure, and even the version of your database. ActiveRecord (the ORM behind Rails) is great for letting us focus on our application and not worry so much about the database particulars.
However, understandingBetter Rails Debugging with better_errors and jazz_handshttp://www.ross-hunter.com/2013/05/17/rails-debugging/2013-05-17T00:00:00Z2022-08-01T11:08:27-04:00Ross HunterGo beyond the stacktrace with 2 gems that grant you debugging superpowers.
Better Errors
Does this screen look familiar? Do you notice anything wrong with this picture?
That is correct, that is the default Rails error page and you are absolutely right, it is completely hideous and hurts to look at. It is begging to be improved.
By answering these questions you have already proven yourself to