RSpec Example Blocks

I recently discovered an interesting test pattern; defining variables in before and after hooks in the rails helper.

A Real Example

Let’s say we are testing a controller and want to make sure only an admin has access to specific views.

When setting admin_signed_in to true in a describe block, we have access to an admin user. I think it is cleaner to set up these users in a hook instead of a shared context or explicitly in the spec.

Hooks

Using Mocks to Test Mailers in RSpec

I was in the situation where I needed to test the delivery of emails based on specific sets of events. I began testing by inspecting sent emails in ActionMailer::Base.deliveries. However, this method of testing felt verbose and sloppy.

RSpec mocks is a great solution when you need to test that a specific method was called. In my case, I want a fundraiser to receive an email after a campaign goal is reached.

The definition for the method and_call_original is, “You can use and_call_original on the fluent interface to "pass through” the received message to the original method.“ What exactly does this mean? It means that when I call funder.fund, I expect CampaignMailer.notify_curator_campaign_goal_reached to be called.

Calling the original method

ENV Variables in XCode

Setting ENV variables in XCode in not straighforward. I have broken down how to accomplish setting up an ENV variable in the following videos and code example.

Make a new conifuration file and set any ENV variables you need.

In the debug environment, use the debug.xcconfig file. Then, assign the value to something, in this case, SignUpUrl corresponds with the SIGN_UP_URL, which is set to localhost:4567/sign_up.

The sign_up_url variable is set to localhost:4567/signup.

Configuring ENV Variables in XCode

Shared Contexts With RSpec

In the past, I used traits with my factories. Below is a simple example of defining a trait:

Using traits

An example spec:

However, I believe that using a shared context is a clearer approach. Defining the amount in a shared context and overriding the amount allows more control over testing use cases.

Using a shared context

I especially like the shared context approach because you can have shared context and redefine attributes as needed in specs.

Auto Layout Xcode for Navigation Bar

Finding auto layout information is difficult. The steps below pin a navigation bar to the top of the screen for all iOS devices.

Steps

• Click the “Pin” button on the bottom right corner
• Click the top, left and right constraints
• Click the Height constraint

The above steps “pin” the navigation bar object to the top of the view and stretch the navigation bar to the sides of the view. The height constraint keeps a static height for the navigation bar.

Auto Layout Basics

Prototypes and Constructors in Javascript

I usually do not need to write code using neither constructors nor prototypes in Javascript. So, I always forget how to write them when I need to use them.

Prototype

Use of ‘prototype’ vs. ‘this’ in JavaScript?

Advantages of using prototype, vs defining methods straight in the constructor?

Shell Function

I have been using youtube-dl every week. The application is a “Small command-line program to download videos from YouTube.com and other video sites.” It’s super useful. However, it default downloads to the current terminal directory. Let’s write a simple shell script in order to place all downloaded files from youtube-dl in the same directory.

The “$” signifies the string following mp3, the example below, “$” is the URL arument.

Now I can run the above script in order to download the video from the youtube URL.!

The Setter Method in a Rails Model

At Fractured Atlas, we are trying to move away from the additional complexity of using callbacks. For example, let’s say we need to do something in a before_save callback such as lowercase a user’s email.

How is this code implemented without using before_save? One method would be overriding the setter accessor.

Now, when the email attribute is updated, my email setter method is called.

Currently, both implementations for downcasing an email are straightforward. However, as the application grows, the before_save callback is more difficult to test and maintain. The before_save is implicit and might have unexpected side effects. Here is a basic spec.

Instead, let’s test the behavior of what we expect.

The first spec is directly testing the callback, whereas the latter spec is testing the behavior of the expected outcome. The latter spec is easier to reason about and change. This is an easy example, but as callbacks grow changing their behavior becomes more difficult. This is especially true if the callbacks are coupled with other models.

What is the right way to override a setter method in Ruby on Rails?

Overwriting default accessors

Ruby's Fetch Method

Checking for values in a Rails params hash is complicated. In this post we will use the #fetch method to ensure nil is not called on a params hash.

The code above will call #present? on nil if params[:search][:organization_id] does not exist.

A better way to write the conditional:

Writing a similar conditional with #fetch:

Let’s break this method down into simpler components:

I like the ability to call #fetch with a default return value if one does not exist. This leads to better ways to handle nil or catch errors.

Swift Classes

Instantiating Person Class

Now, if you copy and paste the the files into a XCode Playground, you can call methods on your person object.

Enums

An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code. - Swift docs

Use an enum when you need a consistent data value.

Structs

A struct allows you to create a structured data type which provides storage of data using properties and extending its functionality via methods. -Tree House