Mocks in Ruby and Zen Test on windows with Growl

These are notes mainly for me, so excuse the jumping around of topics in this post, I have been re-reading the book “Continuous Testing” by Ben Randy and Rod Coffin.

The Twitter gem used in the first example has changed quite a bit since the version used in the book, this was not a problem as a quick read of the documentation showed the way to achieve a similar result using the new API.

require 'twitter'

class User
  attr_accessor :twitter_username

  def last_five_tweets
    return Twitter.search("from:" + @twitter_username, :count => 5).map do |tweet|
      tweet[:text]
    end
  end
end

The line “Twitter.Search” should return 5 tweets from the user with the given user name.

The fun bit came when trying to mock the Twitter call as the original code was totally different, reading the ruby mocking documentation, I discovered that RSpec adds the mocking functions to every object in the application, not sure how this is achieved yet one of those magic bits of Ruby and its ability to extend classes on the fly, but it means that the test code for the above is very simple once the concept is understood.

 it "provides the last five tweets from Twitter" do
      tweets = [
          {:text => 'tweet1'},
          {:text => 'tweet2'},
          {:text => 'tweet3'},
          {:text => 'tweet4'},
          {:text => 'tweet5'},
      ]

      Twitter.should_receive(:search).with("from:logosity",:count=>5).and_return(tweets);

      @user.last_five_tweets.should == %w{tweet1 tweet2 tweet3 tweet4 tweet5}
    end

The line “Twitter.should_receive” is the setup for the mock “Twitter” being the object to be mocked and “search” the method that is expected to be called the “.with” is the arguments expected by the call, “and_return” defines what the mock should return for valid calls. 

Getting the auto test to run was as simple on my windows machine as in the book now we have the excellent rails installer which makes setting up Ruby and Rails on windows easier than any other environment, the first time I read this book, Bundler and “Jeweler” just wouldn’t work on windows at all and required a virtual machine, now everything simply just works, the only bit that caused problems was getting the auto test to call growl for windows to display the results in a nice popup display.

The documentation for “AutoTest Growl for Windows” slightly out of date and the suggested gem installs an older version which didn’t work on my machine throwing an error when autotest was run. 

After some digging around, I cloned this git repository for the growl auto-test project and got my Ruby Mine environment to build and deploy the gem for me, which has worked fine, a quick note growl auto-test sends failing tests messages as priority “Emergency”, and passing test messages as priority “Very Low” so setting the colours for these two priority settings gives the visor a helpful colour

autotest growl setup

Also in the root of the project, same place as the other configuration files like the gem file there needs to be a file called “.autotest” this must have the require for the growl auto-test gem

require 'autotest/growl'

now to carry on reading!

Advertisements

About Duncan Butler

Trying to be a very agile software developer, working in C# with Specflow, Nunit and Machine Specifications, and in the evening having fun with Ruby and Rails
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Mocks in Ruby and Zen Test on windows with Growl

  1. The RapidRep Test Suite represents test automation for the back-end. Typical fields of application include (amongst others): verifying calculation results, reviewing migrations, measuring and controlling data quality, determining and anonymizing test case data, the comparison of databases and the rule-based determination of target results (“test oracle”). Many test objects in the back-end can be tested, also with a high level of automation. In addition, the RapidRep Test Suite can access more than 50 different data sources. These include all relevant databases as well as structured file formats (CSV files, Fixed Length files, XML files, Excel). User-defined Excel workbooks, which can optionally be transferred to a connected test and defect management system, document the test execution and test analysis in compliance with all relevant ISO/IEC/IEEE test standards. Rulebooks, SQL, a script language and Excel formulas are used to realize test implementations.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s