Test Context Builder

Test context builder is an npm package I wrote to complement db-fabricator. The main purpose of db-fabricator is to setup test context data for integration testing. However, setting up test context data gets repetitive and verbose for each test. In many cases you have the same basic object that most of your tests need. So it would be nice to be able to define a set of basic contexts that you often use and be able to build on top of it. This is what test-context-builder is for.

DB Fabricator 2.0

It hasn’t been that long since I released DB Fabricator version 1.0. But I quickly realized the limitation as I use it more on my project. The biggest annoyance is the fact that when you want to fabricate an object that depends on another fabricated object, you have to fabricate the dependency after resolving the root object promise, resulting in a somewhat verbose nested code like this:

Introducing DB Fabricator

When writing end to end tests, we often need to set up a context by creating some data in database. Each test case should start with a clean slate and have data that are created only for that test. So it’s important to make creating these test data as simple as possible.

Using Shared Examples in RSpec

Shared examples is a great RSpec feature that allows you to reuse test cases in different context. One use case is when you need to test different access level to a resource depending on the role. Lets say you have a resource that can have published/draft state. There are several roles with different access level to this resource.

Helper to print out your ActiveRecord SQL

Sometime you just want to see the SQL statement that your ActiveRecord magically generate, without going through hundreds of lines in your log file. An easy way to do that is to assign the ActiveRecord::Base.logger to a new Logger that prints to the STDOUT, and then change it back to the old logger so it doesn’t keep printing out SQL for the rest of your code.

Ruby Tempfile and Garbage Collection

Tempfile gives you an easy way to quickly create a temporary file without having to worry about generating a file name that doesn’t already exist and cleaning up the file after you are done. However, you have to be aware that the temporary file will be deleted when the tempfile instance is garbage collected.

How to test file upload with Grape Framework

Grape is a Rack based Ruby framework for building API. It’s only a few years old and has not reached version 1.0 yet. The main github page has a very good documentation but is still not as complete as Rails guide. In this post, I will discuss how to test file upload endpoint with RSpec.

Stop using backtick to run shell command in Ruby

It’s all too common to find the use of backtick to run shell command from Ruby. It’s fine when you just run a command that doesn’t take any user input. But when you start passing input from untrusted source, that’s when the trouble begins.

Comparing ancestry and closure_tree for your nested data structure

When it comes to implementing ActiveRecord nesting, there are a few popular implementations. In this post I will look closer at how Ancestry and Closure tree work and what the pros and cons are.

What you need to know about gitignore pattern

After using git for a long time, I thought I knew the basic of gitignore well enough, until I ran into problem with my files not getting committed. There were some basic gitignore patterns that most git users are just not aware of. So some of us might have put something on gitignore without realizing that it does ignore a lot more stuff than we intend.