Assertions in Moq Callbacks

I learned an important lesson today: do not assert inside of Moq callbacks. In the code below, even if the Assert.AreEqual fails, it will not cause the unit test to fail.

moqObj.Setup(m => m.Doit(It.IsAny<string>())).Callback<string>((s) => 
    Assert.AreEqual("foo", s);
// Insert test code that invokes `Doit`

You could use Verify instead, but it’s not always ideal, particularly when trying to step through the assertions while debugging tests. Instead, you can take this approach suggested by Thomas Ardal.

string actual = null;
moqObj.Setup(m => m.Doit(It.IsAny<string>())).Callback<string>((s) => 
    actual = s;
// Insert test code that invokes `Doit`
Assert.AreEqual("foo", actual);

A New Look for 2015

I have wanted to revamp my website for close to a year now. In that time I’ve looked into different blogging engines and tried creating new themes, but I didn’t settle on a path forward until a couple weeks ago.


At one point I considered moving to a statically generated site since they were all the rage. I tried out Jekyll and Middleman. I liked both and even wrote up a post on using Zurb’s Foundation with Middleman. However, I decided that by sticking with WordPress I could focus more on creating content.


Something that I really like about the static site generators is their first-class support for Markdown. I routinely write Markdown, so I felt that if I could generate my posts in Markdown, I would have one less hurdle for generating content. Before, I was using the WP-Markdown plugin, but I don’t like that it doesn’t retain the original Markdown content. Instead, it generates and saves the HTML when the post is saved, then converts it back to Markdown when editing the post. This probably works for most people, but I was having trouble with some of my code blocks getting messed up during the conversions. I decided to start using the Markdown feature in the Jetpack plugin. This plugin retains the original Markdown, and I’ve been very happy with it so far. This plugin also works well highlight.js, which I’m using for syntax highlight on the site.


The biggest problem I have had so far with maintaining my blog is generating posts. Over 3 years I have only published 31 posts, and only 2 in 2014. So, I decided to be more deliberate about pulling together post ideas and notes. I created a Trello board where I’m maintaining a list of post ideas and collecting notes for posts that I’m working on. I use other Trello boards for other purposes and have had a lot of success with them, so I’m hopeful that this will improve my content creation process.


I really liked the new Twenty Fifteen theme for WordPress, which is why I decided to base my new theme on it. I created the Block 15 child theme to add some customizations for my site. It has a lot of stuff hardcoded specifically for my website, but I decided to open source it on GitHub in case it could help anyone else interested in creating their own child theme.


I decided to drop my use of Disqus with the new theme. The Disqus thread always looked so ugly on the site and significantly increased the page load time. Instead, I added a Twitter link at the bottom of each post, encouraging readers to start a conversation about the post on Twitter. In addition to avoiding an ugly, long discussion thread on posts, this also increases my site’s overall visibility. Some users that have never been to my might observe a conversation on Twitter, and decide to check out my site as a result.

So, here’s to better year of blogging in 2015.

Google Analytics Site Speed for Small Websites

If you’re running a small website and using Google Analytics, your site speed metrics graph might look empty. If you don’t get a lot of traffic there will be no samples to measure site performance. This is because the default sampling rate is only 1% (i.e. site speed metrics will only be collected for 1% of your visitors). This is plenty if your running a big site with tons of visitors per day, but if your running a smaller site this isn’t going to be good enough.

Luckily, Google Analytics allows you to change this setting. If you’re using Universal Analytics you can set the site speed sample rate when creating your tracker. In the example below I have set the tracker to sample 50% of my visitors by passing {'siteSpeedSampleRate': 50} in the create call.

<script type="text/javascript">
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

ga('create', 'UA-XXXX-Y', {'siteSpeedSampleRate': 50});
ga('send', 'pageview');

After doing this I got a significant increase in the number of samples per day, allowing me to get a better idea of how my site was performing.

Middleman Foundation

This weekend I decided to try out the static site generator, Middleman. While I was working on converting an existing static site, I decided to take the opportunity to try out Zurb’s Foundation and SCSS too. Andrea Moretti created a Middleman template to get you started doing just this, but I hate just using templates without understanding what they are doing. So I decided to start with Middleman’s HTML5 Boilerplate template and add the Foundation Bower package to it. You can check out the final product over on GitHub at If you’re not interested in the inner workings, grab Andrea Morretti‘s Middleman Foundation template.

Middleman Template and Bower Setup

Initialize the project with the HTML5 Boilerplate.

> middleman init middleman-foundation --template=html5
> cd middleman-foundation

Create a .bowerrc file to specify a the install location for bower components

// .bowerrc
    "directory" : "source/bower_components"

Initialize bower for the project.

> bower init

Install Foundation.

> bower install foundation --save

Add the Compass configuration to config.rb.

# config.rb
compass_config do |config|
  # Require any additional compass plugins here.
  config.add_import_path "bower_components/foundation/scss"

  config.output_style = :compact

Add the bower directory to the Sprockets asset path in the config.rb.

# config.rb
# Add bower's directory to sprockets asset path
after_configuration do
  @bower_config = JSON.parse("#{root}/.bowerrc"))
  sprockets.append_path File.join "#{root}", @bower_config["directory"]

Replace CSS with SCSS

Delete source/css/main.css and source/css/normalize.css. In their place add a new file, all.css.scss.

# all.css.scss
@import "normalize.scss";
@import "foundation.scss";

Replace the stylesheet references in the <head> of source/layouts/layout.erb with the all.css.scss.

<!-- source/layouts/layouts.erb -->
<%= stylesheet_link_tag "all" %>

Use Modernizr from Bower

Create a source/js/modernizr.js with the following content to include the modernizr source from the installed bower components.

// source/js/modernizr.js
//= require modernizr/modernizr

Then replace the modernizr reference in layouts.erb‘s head with <%= javascript_include_tag "modernizr" %>.

Use Other Javascripts from Bower

Delete all files (except the new modernizr.js) in source/js/, and create all.js with the following contents to include necessary javascripts from the bower components.

// source/js/all.js
//= require jquery/dist/jquery
//= require foundation/js/foundation.min

Then, update layouts.erb to reference your new all.js and remove the old javascript references.

<!-- source/layouts/layout.erb -->
<%= javascript_include_tag  "all" %>


You’re all ready to go. Launch middleman server and start building!