<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Transcending Frontiers &#187; RSpec 2.6.0 and RCov</title>
	<atom:link href="http://blog.thefrontiergroup.com.au/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.thefrontiergroup.com.au</link>
	<description>Your peek inside the collective mind of The Frontier Group</description>
	<lastBuildDate>Tue, 03 Jan 2012 06:53:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>RSpec 2.6.0 and RCov</title>
		<link>http://blog.thefrontiergroup.com.au/2011/05/rspec-2-6-0-and-rcov/</link>
		<comments>http://blog.thefrontiergroup.com.au/2011/05/rspec-2-6-0-and-rcov/#comments</comments>
		<pubDate>Thu, 19 May 2011 11:02:19 +0000</pubDate>
		<dc:creator>Mario Visic</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[rcov]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog.thefrontiergroup.com.au/?p=1510</guid>
		<description><![CDATA[Just upgraded to the newest RSpec (2.6.0) and found that RCov has stopped working completely? That&#8217;s what happened to me after running a bundle update. RCov refused to run, no error messages, just blank output. After looking around for a little while I found this: https://github.com/rspec/rspec-core/issues/370 In short: the new RSpec has been broken up [...]]]></description>
			<content:encoded><![CDATA[<p>Just upgraded to the newest RSpec (2.6.0) and found that RCov has stopped working completely? That&#8217;s what happened to me after running a bundle update. RCov refused to run, no error messages, just blank output.</p>
<p>After looking around for a little while I found this: <a href="https://github.com/rspec/rspec-core/issues/370">https://github.com/rspec/rspec-core/issues/370</a></p>
<p>In short: the new RSpec has been broken up into modules a tad more, the one that we require for rspec to run correctly <em>&#8216;autorun&#8217;</em> is not included by default, so to solve this simply add <em>require &#8216;rspec/autorun&#8217;</em> to the top of your spec_helper.</p>
<p class="gist-block" data-gist-id="978300" data-gist-file="spec_helper.rb" id="gist-978300">Can&rsquo;t see this Gist? <a rel="nofollow" href="http://gist.github.com/978300">View it on Github!</a></p>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2011/05/rspec-2-6-0-and-rcov/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2011/05/rspec-2-6-0-and-rcov/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Specjour with Custom Bundler and Database Setup</title>
		<link>http://blog.thefrontiergroup.com.au/2010/06/specjour-with-custom-bundler-and-database-setup/</link>
		<comments>http://blog.thefrontiergroup.com.au/2010/06/specjour-with-custom-bundler-and-database-setup/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 05:51:27 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Inside TFG]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Specjour]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://thefrontiergroup.com.au/blog/?p=811</guid>
		<description><![CDATA[We have a test suite here that now is rapidly approaching 2 hours using a single core. Let me just repeat that. A developer realistically would have to leave their machine testing overnight to see if the suite is working. That&#8217;s really not good enough. Specjour has been a bit of a turn-key miracle worker [...]]]></description>
			<content:encoded><![CDATA[<p>We have a test suite here that now is rapidly approaching 2 hours using a single core. Let me just repeat that. A developer realistically would have to leave their machine testing overnight to see if the suite is working. That&#8217;s really not good enough.</p>
<p><a href="http://github.com/sandro/specjour">Specjour</a> has been a bit of a turn-key miracle worker with our RSpec suite, however lately we&#8217;ve started to require some custom database setup that we do in a seeds.rb file as well as some custom bundler install parameters as most of our devs don&#8217;t have MySQL installed. Both of our needs were being nicely stomped on by Specjour so I thought it was time to look elsewhere.</p>
<p>I took a trip down <a href="http://wiki.github.com/ngauthier/hydra/">Hydra</a> lane and while getting to the point of having a working, local, dual runner system was a piece of cake, getting something working remotely via SSH took me hours of pain. Debugging the remote SSH workers was a nightmare and I spent a couple of hours running through code before deciding it was probably better to update our existing solution rather than tooling up a brand new one.</p>
<p>Back to the Specjour code. Specjour includes a rails directory inside which is an init.rb which Rails will run at initialisation (it&#8217;s part of what Rails does) but the Specjour initialiser will always just run the default database setup task no matter what initialiser you&#8217;ve got setup. We had a specjour initialiser that runs if ENV['PREPARE_DB'] was populated, which it is by Specjour, the problem was that the Specjour initialiser ran in the Rails <code>after_initialization</code> hook and therefore stomped all over our database setup.</p>
<p>The first step was just to have our initialiser write to another ENV element and then to have the Specjour <code>after_initialize</code> handler respect this. This isn&#8217;t too hard to implement as the <code>after_initialize</code> handler is just a block that is attached and so inside of this block you just need to check that ENV element. In my case I created a new ENV['DB_PREPPED'] element when my database setup had completed and then when the <code>after_initialize</code> block runs it checks for ENV['DB_PREPPED'] and will do nothing if that&#8217;s been set to true.</p>
<p>Easy. I now had Specjour respecting our database setup task.</p>
<p>The next step was to try and test this outside of a Rails application, not only that but to test the operation of a block (anonymous function?). To do this I setup a stub on a mock Rails class and let it capture the <code>after_initialize</code> block and then I ran a number of specs against this block.</p>
<pre>module Specjour
  module DbScrub
  end
end

DO_NOT_REQUIRE = true

describe "Rails Initialiser" do
  before :all do
    ENV['PREPARE_DB'] = "true"

    stub(Specjour::DbScrub).scrub

    class Rails
      class &lt;&lt; self; attr_accessor :configuration; end
      class &lt;&lt; self; attr_accessor :test_block; end
    end

    config = Object.new
    stub(config).after_initialize { |args|
      object = Object.new
      Rails.test_block = args
      object
    }
    Rails.configuration = config

    require 'rails/init'
  end

... tests ...</pre>
<p>This code essentially mocks up Rails.configuration and then stubs the <code>after_initialize</code> method. This stub then places the block that <code>after_initialize</code> yields to into Rails.test_block. When I <code>require 'rails/init'</code> it sequentially processes the file (as with all Ruby) and the stub will capture the block. After this is a bunch of tests I run an whether the <code>Specjour::DbScrub.scrub</code> method is called or not, so it&#8217;s nothing special.</p>
<p>I felt like at this stage I had fairly well tested the main aspects of the database setup.</p>
<p>The next issue was with how bundler was being handled. We have a situation where we would like to install sometimes without some gems. Some of the gems we use and have written use applications we&#8217;d rather not maintain in development and get tested in our staging and production environments. We generally will run a bundle install in development without the production or metrics groups so I wanted to have the ability to pass through a custom bundler command. That&#8217;s pretty easy now with my gem. Inside .specjour/bundler.yml there is a command property. I think this is more complex than what&#8217;s required, but I can foresee us needing a number of custom rake tasks and shell scripts so this bundler.yml should have probably started life as a settings/commands/something_generic.yml</p>
<p>To test this part of my changes was pretty simple. I basically just stubbed the system calls to bundler to give certain return values and checked to make sure the correct program flow happened.</p>
<pre>describe ".bundle_install" do
    let :manager do
      stub.instance_of(Specjour::Manager).project_path { "/tmp" }

      stub(Dir).chdir(anything) { |args|
        args.last.call # This yields to the block for Dir.chdir()
      }

      manager = Specjour::Manager.new
      stub(manager).project_path { "blah" }
      mock(manager).system('bundle lock')

      manager
    end

    it "should perform a bundle lock" do
      stub(manager).system('bundle check &gt; /dev/null') { true }

      manager.bundle_install
    end

    it "should check if there are gems required" do
      mock(manager).system('bundle check &gt; /dev/null') { true }

      manager.bundle_install
    end

    context "when gems are required" do
      before :each do
        # Not a before :all as it needs to hook into the let hook above

        stub(manager).system('bundle check &gt; /dev/null') { false }
      end

      context "and there is a bundler YAML file" do
        before :each do
          config_file = ".specjour/bundler.yml"

          mock(File).exists?(config_file) { true }
          mock(File).read(config_file) { "" }
          mock(YAML).load(anything) {
            { 'command' =&gt; "do it" }
          }
        end

        it "should get the bundle command from the YAML file" do
          mock(manager).system('do it &gt; /dev/null')
          manager.bundle_install
        end
      end

      context "and there is no bundler YAML file" do
        before :each do
          mock(File).exists?(".specjour/bundler.yml") { false }
        end

        it "should perform a bundle install" do
          mock(manager).system('bundle install &gt; /dev/null')
          manager.bundle_install
        end
      end
    end
  end</pre>
<p>You can see that I stubbed our the <code>Dir.chdir</code> block to just yield directly to the call, otherwise it&#8217;ll throw an exception. Then I stubbed and mocked out the <code>Kernel.system</code> calls as necessary. Kernel methods are generally included into Ruby objects so you don&#8217;t stub Kernel, you stub the object that has the Kernel methods. Most of the testing is pretty basic, but I&#8217;d be keen to hear if I&#8217;m doing anything incorrectly!</p>
<p>This was my first major venture into adding functionality to a public project and it was good fun. I think it made me do a little better work than I might normally, it&#8217;s a great motivation to potentially have peers look at how you do things.</p>
<p>After bundling it all up and testing it here with over a dozen developers and even more machines I&#8217;m pretty happy with how it functions. I&#8217;ve made a pull request back to the original gem creator and hopefully he&#8217;ll like what I&#8217;ve done. In the meantime if you want to check it out then my Specjour is available on <a href="http://github.com/ozzyaaron/specjour">Git Hub</a>.</p>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2010/06/specjour-with-custom-bundler-and-database-setup/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2010/06/specjour-with-custom-bundler-and-database-setup/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using RSpec Example Groups for Common Functionality</title>
		<link>http://blog.thefrontiergroup.com.au/2010/01/using-rspec-example-groups-for-common-functionality/</link>
		<comments>http://blog.thefrontiergroup.com.au/2010/01/using-rspec-example-groups-for-common-functionality/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 00:58:53 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Inside TFG]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://thefrontiergroup.com.au/blog/?p=624</guid>
		<description><![CDATA[I&#8217;m currently getting into using RSpec for testing our controllers on what is turning into a large project. It&#8217;s been more than handy because we have a lot of complex scoping to take into account whenever retrieving data. People don&#8217;t like to see other peoples&#8217; financial data, mostly because it implies that someone is probably [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently getting into using RSpec for testing our controllers on what is turning into a large project. It&#8217;s been more than handy because we have a lot of complex scoping to take into account whenever retrieving data. People don&#8217;t like to see other peoples&#8217; financial data, mostly because it implies that someone is probably looking at theirs. With this in mind it&#8217;s more than important that we know the right data is going to the right places and hence the need for controller testing. </p>
<p>Now most of our controllers require the user to be logged in so writing tests to check this for every controller is annoying and time consuming, more than that it feels dirty. I think this is what some people call a <em>code smell</em> though I&#8217;m not up to speed on buzz words. There are also other tasks that are done quite often such as setting up the various types of users we&#8217;d like to test as, it would be nice if this were easily put in one place and could be easily pulled in. I guess I was looking for a template of tests that I could share. </p>
<p>It seems that the solution to it is found in Shared Example Groups which I hadn&#8217;t heard very much discussion about and it kind of leaves working out how they work to you rather than documenting it too much. </p>
<p>So far I&#8217;ve used it simply to make sure that controllers that require are redirecting users appropriately and also for setting up a specific type of user for our system before testing. </p>
<p>I created a directory under /spec/support called example_groups and in there I have a file called login_groups.rb. In that file I have something like the following : </p>
<pre class="ruby">
shared_examples_for "customer is logged in" do
  before(:all) do
    @user = Factory(:customer_user)
    @user.customers.push Factory(:customer)
  end

  before(:each) do
    activate_authlogic
    OperatorSession.create(@operator)
  end
end
</pre>
<p>Now in my spec files when I have a bunch of tests requiring a logged in customer I will include this little snippet : </p>
<pre class="ruby">
  it_should_behave_like "customer is logged in"
</pre>
<p>I get a logged in customer to start playing around with. I have the spec/support/example_groups directory in my include paths for Rspec and so it just all works. </p>
<p>My tests can then start to look like : </p>
<pre class="ruby">
describe MerchantsController do
  it_should_behave_like "areas requiring login"

  context "customer logged in" do
    it_should_behave_like "customer is logged in"

    ... insert other tests here ...
  end
end
</pre>
<p>It means I can swap in another authentication gem/plugin pretty easily and also encapsulates the logic about creating customers, or whatever type of item you want to use, so that if that changes you can swap things in and out with a minimum of fuss. </p>
<p>Just to be clear, example groups aren&#8217;t limited to setup tasks or connecting to before/after hooks, you can also include a bunch of tests as well. This allows me to have a bunch of tests to run to make sure that a user does have to be logged in for various controllers and include these tests in on line. </p>
<p>I hope this helps someone, it took a bit of searching and trial and error myself this morning to get it working and find the uses for it that I&#8217;ve found. I&#8217;m definitely open to better solutions to this sort of issue though. </p>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2010/01/using-rspec-example-groups-for-common-functionality/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2010/01/using-rspec-example-groups-for-common-functionality/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Testing File Uploads with Webrat and Paperclip</title>
		<link>http://blog.thefrontiergroup.com.au/2009/06/testing-file-uploads-with-webrat-and-paperclip/</link>
		<comments>http://blog.thefrontiergroup.com.au/2009/06/testing-file-uploads-with-webrat-and-paperclip/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 03:13:23 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[Cucumber]]></category>
		<category><![CDATA[Paperclip]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Webrat]]></category>

		<guid isPermaLink="false">http://thefrontiergroup.com.au/blog/?p=389</guid>
		<description><![CDATA[&#8211; Check out some of our more recent Ruby on Rails blog posts. If you&#8217;d like to hire our team, get in touch &#8211; I wanted to integrate some branding functionality into an application we&#8217;re developing and so I needed test file upload functionality. We&#8217;re using Webrat for integration tests, though this will likely change as [...]]]></description>
			<content:encoded><![CDATA[<p><em style="font-style: italic;">&#8211; Check out some of our more recent </em><a title="Ruby on Rails posts" href="http://thefrontiergroup.com.au/blog/category/ruby-on-rails/"><em style="font-style: italic;">Ruby on Rails blog posts</em></a><em style="font-style: italic;">. If you&#8217;d like to hire our team, <a title="Get in touch" href="http://thefrontiergroup.com.au/pages/contact-us">get in touch</a></em><em style="font-style: italic;"> &#8211;</em></p>
<p>I wanted to integrate some branding functionality into an application we&#8217;re developing and so I needed test file upload functionality. We&#8217;re using Webrat for integration tests, though this will likely change as we increase the amount of Javascript in the app. I added Paperclip to handle the file attachments for logos, and everything was working.</p>
<p>When I added validation to the model, making sure that the file being attached was an image, this broke the tests. It didn&#8217;t seem to matter what type the file was, it would fail no matter what on the file type validation.</p>
<p>I used ruby-debug to debug my test and it seems by default Webrat sends file uploads as plain text. It does have the option to specify the file type when attaching the file, so the easiest way around this is just to specify the MIME type for the file. Now my Cucumber step looks something like this :</p>
<pre class="ruby">When /^I attach "([^\"]*)" image to the "([^\"]*)" file field$/ do |filename, field|
  type = filename.split(".")[1]

  if type == "jpg"
    type = "image/jpeg"
  end

  attach_file field, File.join(RAILS_ROOT, test_asset_path, filename), type
end</pre>
<p>Obviously this will need some work as I progress, but it works. At this stage I have an assets folder in my features folder to store any files that I need for my tests.</p>
<p>On the confirmation end of the test I just have a simple tag test to check that the image tag is displaying, and it contains the correct src attribute :</p>
<pre class="ruby">Then /^I should see tag "(.+)"$/ do |selector|
  (Hpricot(response.body)/selector).should_not be_empty
end</pre>
<p>So in my feature test I have :</p>
<pre class="ruby">Then I should see tag "img[@src*='']"</pre>
<p>This just confirms that there is an image tag that contains the file name of the file that I uploaded in the test.</p>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2009/06/testing-file-uploads-with-webrat-and-paperclip/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2009/06/testing-file-uploads-with-webrat-and-paperclip/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Making a Copy of an Object in PHP</title>
		<link>http://blog.thefrontiergroup.com.au/2009/02/making-a-copy-of-an-object-in-php/</link>
		<comments>http://blog.thefrontiergroup.com.au/2009/02/making-a-copy-of-an-object-in-php/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 02:46:35 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHPUnit]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://thefrontiergroup.com.au/blog/?p=326</guid>
		<description><![CDATA[In PHP4 objects were passed by value, it&#8217;s probably the intuitive way to deal with variables for a beginner and in a language where objects are not first class. However in PHP5 this has been changed and now objects are passed by reference, this stung me when writing some tests recently. public function testNameIsUnique() { [...]]]></description>
			<content:encoded><![CDATA[<p>In PHP4 objects were passed by value, it&#8217;s probably the intuitive way to deal with variables for a beginner and in a language where objects are not first class. However in PHP5 this has been changed and now objects are passed by reference, this stung me when writing some tests recently. </p>
<pre class="php">
public function testNameIsUnique() {
	$test1 = $this->BuildValidDiscountType();
	$test2 = $test1;

	$test1->Save();
	$this->assertTrue($test1->id > 0);

	$this->setExpectedException('DiscountTypeException');
	$test2->Save();
}
</pre>
<p>This code shouldn&#8217;t have worked as far as I was concerned, in fact I was expecting an exception to be raised. Instead it was working and after a little debug tour I found that my <code>$test1 = $test2</code> line was causing <code>$test2</code> to be a reference to <code>$test1</code>, not what I wanted. This caused my update method to be triggered instead and of course the name is still unique. </p>
<p>It only required a small change to that line, using the <a href="http://php.net/clone">clone</a> specifier : </p>
<pre class="php">
$test1 = clone $test2;
</pre>
<p>After that everything went as expected and I knocked off another test, and added to my PHP knowledge.</p>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2009/02/making-a-copy-of-an-object-in-php/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2009/02/making-a-copy-of-an-object-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

