Tuesday, September 19, 2017

Stubs, Mocks, and Spies in Rspec

There's a number of different terms we use in testing to describe how we're setting up the tests. Are we stubbing? Mocking? Spying? All of the above? What do they even mean?

1. Stubs

These are just canned responses. You stub out methods so that when they're called, they just return something (anything you want). If they're not called, nothing happens. 

Rspec stubbing: https://relishapp.com/rspec/rspec-mocks/v/2-14/docs/method-stubs

Example

allow(obj).to receive(:message).and_return(:value)

You can do this either both real objects and doubles.

2. Spies

These are objects that you set up with canned responses (like stubs) that also record information about the calls made to that method (was it called? what was it called with? How many times?). 

Rspec

Example

allow(Invitation).to receive(:deliver) 

or

invitation = spy('invitation')

Followed by the assertion

expect(invitation).to have_received(:deliver)

3. Mocks

These are objects that have already been instrumented with expectations. They're like spies in that actions are recorded but they also go a step further to automatically verify the behavior when the test is exercised.

Example

logger = double("logger")
account = Account.new logger

expect(logger).to receive(:account_closed)

account.close

State verification vs Behavior verification 

Verification is the act of verifying that something occurred in some manner. In testing, we rely on either state verification or behavior verification to ensure that objects are behaving in the way we expect them to. 

In the testing world, state verification is verifying the test through the state of an object. What's the current value of variable X? In other words, what's the current status of this object? Does it match up with expectations? 

Behavior verification takes a different approach. Behavior verification is not concerned with what state the object is in - it's only concerned with the fact that certain actions occurred in some manner (regardless of how they changed the objects state). 

According to Martin Fowler, only mocks insist on behavior verification. In other words, unlike stubs and spies, only mocks are preprogrammed to perform behavior verification. With mocks, you don't really have a choice to do state verification. 






Sunday, September 10, 2017

Spinning up a rails app using Heroku in < 5 min

It's really amazing how you can have an app running in production in < 5 minutes using Heroku. Below are the minimal set of required steps to serving a basic static page to Heroku.

Pre-requisites
  • Heroku Account
  • Heroku CLI
  • Rails CLI
Create and and commit the app

rails new app-0 -d postgresql && cd app-0

rails generate controller welcome

touch app/views/welcome/index.html.erb && echo 'Hello World' > app/views/welcome/index.html.erb

Add root 'welcome#index' to the config/routes.rb file 

git add . && git commit -m 'first commit'

Deploy app to Heroku

heroku login

heroku create

git push heroku master

heroku ps:scale web=1

heroku open

Topics:

  • GIT
  • PostgreSQL
  • Rails
    • Routing
    • Code generation
  • Heroku
    • create
    • ps
    • open
    • Default Rails Server: WEBrick
      • Single threaded server vs multithreaded server


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.






Saturday, July 29, 2017

Smoothie breakfast diet

I'm currently experimenting with a low-sugar, nutrient-dense breakfast smoothie diet that's easy to make. This is a list of three smoothies that I will be having throughout the week along with a grocery list of ingredients you'll need to buy to make them. 


Banana Peanut butter 

http://www.youmustlovefood.com/banana-peanut-butter-cinnamon-smoothie/
http://allrecipes.com/recipe/221261/peanut-butter-banana-smoothie/A
  • Peanut butter 
    • has protein as well as potassium — which lowers the risk of high blood pressure, stroke and heart disease. It also contains fiber for your bowel health, healthy fats, magnesium to fortify your bones and muscles, Vitamin E and antioxidants.
  • Bananas
    • They contain several essential nutrients, and have benefits for digestion, heart health and weight loss.
    • Bananas are among the most popular fruits on earth.
    • Bananas contain a fair amount of fiber, as well as several antioxidants.
Grocery List 
  • banana 2x
  • milk (2 cups)
  • peanutbutter (1/2 cup)
  • Cinnamon powder
  • Cacao powder 
    • https://iquitsugar.com/raw-cacao-vs-cocoa-whats-the-difference/
Apple Strawberry

http://gimmedelicious.com/2015/07/16/apple-strawberry-smoothie/
  • Apples are 
    • extremely rich in important antioxidants, flavanoids, and dietary fiber. The phytonutrients and antioxidants in apples may help reduce the risk of developing cancer, hypertension, diabetes, and heart disease. This article provides a nutritional profile of the fruit and its possible health benefits.
  • Strawberries are no exception to this rule; 
    • in addition to antioxidants, they have many other nutrients, vitamins, and minerals that contribute to overall health. These include folate, potassium, manganese, dietary fiber, and magnesium. It is also extremely high in vitamin C!
Groceries
  • Apple (1x)
  • Frozen strawberries (1 cup)
  • Milk (1/2 cup)
  • Strawberries / banana (1 cup)


Avocado Coconut

https://iquitsugar.com/recipe/avocado-coconut-dreamboat-smoothie/
  • Avocados are 
    • Health benefits and nutritional information. Also known as an alligator pear or butter fruit, the versatile avocado is the only fruit that provides a substantial amount of healthy monounsaturated fatty acids (MUFA). Avocados are a naturally nutrient-dense food and contain nearly 20 vitamins and minerals.
  • Coconut oil
    •  is high in natural saturated fats. Saturated fats not only increase the healthy cholesterol (known as HDL cholesterol) in your body, but also help convert the LDL “bad” cholesterol into good cholesterols. By Increasing the HDL in the body, it helps promote heart health and lower the risk of heart disease.
Groceries
  • Coconut oil (2 tablespoons)
  • Blueberries (1 / 2 cup)
  • Banana (frozen) ( 1) 
  • Avocado (1)
  • Coconut cream (500 ml)
Toppings
  • Gogi berries
    • https://www.downtoearth.org/health/vitamins-supplements/ways-to-use-goji-berries
  • Chia seeds
  • Cacao nibs 
  • Mulberries 
Final grocery list
  • 2 Apples
  • 4-5 Bananas 
  • Cacao powder
  • Coconut oil 
  • Coconut cream (2 cups)
  • 1 Avocado 
  • 1 container Blueberries
  • Strawberries 
  • Frozen strawberries
  • Almond milk 

Tuesday, May 16, 2017

How to run Cinemania 96 on Windows 10


  1. Install Virtualbox 
  2. Create a windows 7 VM using free IE8 windows 7 virtual machine from microsoft. Create it with at least 1GB of RAM so it's no super slow.
  3. Download virtualbox guest addition and extension pack
  4. Add the extension pack to VB
  5. Start windows 7 VM and install guest additions. Reboot.
  6. Connect CD to USB port
  7. Add a USB device in the VM
  8. Copy files over to the hardisk 
  9. Run cinemania.exe