Sunday, August 20, 2017

Rspec doubles - normal double, instance double, class doublea

Test doubles are any object that are suppose to stand in and represent real objects during testing. Rspec offers three types of doubles - ordinary doubles(just double), instance doubles, and class doubles.

All doubles are strict by default. What that means is that if any un-allowed or unexpected methods are invoked, the test will fail. For example:

x = double()
a.bar // error because `bar` was not allowed.

However, you can make any double loose by appending `as_null_object`.

Now lets look at differences.

Ordinary doubles

x = double()

These doubles are super barebones. You can allow any messages on these methods.

Instance doubles

x = instance_double('ClassName')

These doubles are aware of the instance methods of class 'ClassName' - you can only allow messages that are defined.

Class doubles

x = class_double('ClassName')

These doubles are aware of the class / module methods of any class or module named 'ClassName'. Just like instance doubles, only defined messages are allowable.

In short, instance and class doubles go a step farther than verifying the state of an object (was 'X' called?). They also verify behavior (is 'X' a thing that this object does?).

When do you use one over the other?

I see ordinary doubles as good for creating dummy objects during a test. For example, if you need to fulfill a parameter requirement for a method where you know the object isn't being used. However, you generally want to use instance and class doubles for the stricter check.

Saturday, August 12, 2017

New Smoothie I'm trying this week

So the avocado coconut smoothie I mentioned last week is more complicated to make than I prefer.

This week I'm going to give this a shot: https://iquitsugar.com/recipe/liver-detox-smoothie/

Ingredients

 small green apple, diced and frozen.
1 zucchini, diced and frozen.
1/4 avocado, diced and frozen.
1 cup mixed greens like broccoli florets, watercress, beetroot greens, silverbeet, kale or spinach.
1/4 cup coriander or parsley (or both!).
1 teaspoon chia seeds.
1/4 teaspoon turmeric, ground.
1/2 lemon, juiced.
2 cups coconut water.

My Grocery List

  • 1 - 2 green apples
  • Zucchini
  • Avocados
  • broccoli florets 
  • Almond Milk

Reflections on Sublime so far - quotes and projects

I switched over to using Sublime from Vim this past year and it's been an absolute joy to use. In fact, I liked it so much I even made a cheatsheet for others to get the most out of this editor. However, there's were still a couple of actions that have been a source of frustration: changing quote types and navigation across multiple projects. In this post I'll discuss what they are, the solution I've adopted to address them, and how I still felt about the solutions after a week of use.

Problem 1: Quotes


Replacing single quotes with double quotes or double quotes with single quotes using multiple selection. If you select both quotes, typing in a single quote will simple quote the double quotes themselves. 

For example:

"hello world" becomes '"'hello world'"' when what I really want is 'hello world'. This isn't a problem if I'm trying to actually quote selections, but very rarely do I (or anyone) want to be quoting the quotation marks. 

Solution

The most promising solution I found to this problem is the ToggleQuotes sublime plugin. I just installed it and it works pretty well. I'm considering adding tests to this and adding support for quotes around multi-line strings. 

Problem 2: Project navigation


When I have sublime opened for > 1 projects, it's difficult to go from one to another. Right now I just use the basic mac application switcher and it works fine for two projects but once I have > 3 applications open it becomes a nightmare.

Solution

Turns out Sublime has a built in projects feature to deal with this issue. I just learned that every window you open is either a named project or anonymous project (if you open sublime in any directory not associated with a project). You can define project specific sublime settings and switch between them quickly using sublimes Project feature. You can also add folders from other projects into the current project.

Update after a week of using ToggleQuotes and sublime projects features

  • The project feature for sublime is absolutely indispensable. I've been able to switch from one project to another seamlessly and this has been a tremendous boost for my workflow. I will also say that I continue to advocate for keeping the project files in the same place - no need to pollute your repos if you don't have to. 
  • Toggle quotes is awesome. Thank you @spadgos.