<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>adams.co.tt blog - tagged with underscore</title>
  <id>http://adams.co.tt/</id>
  <updated>2012-03-14</updated>
  <author>
    <name></name>
  </author>
  <entry>
    <title>Updated jasmine-underscore</title>
    <link rel="alternate" href="http://adams.co.tt/blog/2012/03/14/updated-jasmine-underscore/"/>
    <id>http://adams.co.tt/blog/2012/03/14/updated-jasmine-underscore/</id>
    <published>2012-03-14</published>
    <updated>2012-03-14</updated>
    <author>
      <name></name>
    </author>
    <content type="html">&lt;p&gt;Last year &lt;a href="https://github.com/adscott/jasmine-underscore"&gt;jasmine-underscore&lt;/a&gt; was &lt;a href="http://adams.co.tt/blog/2011/06/19/jasmine-underscore/"&gt;released&lt;/a&gt;. Since then, some issues have been fixed, and two new matchers have also been included:&lt;/p&gt;

&lt;h5&gt;allToSatisfy&lt;/h5&gt;

&lt;pre&gt;&lt;code&gt;expect([2, 4, 6]).allToSatisfy(function (val) { return val%2 == 0; });
expect([1, 4, 6]).not.allToSatisfy(function (val) { return val%2 == 0; });
&lt;/code&gt;&lt;/pre&gt;

&lt;h5&gt;anyToSatisfy&lt;/h5&gt;

&lt;pre&gt;&lt;code&gt;expect([2, 3, 5]).anyToSatisfy(function (val) { return val%2 == 0; });
expect([1, 3, 5]).not.anyToSatisfy(function (val) { return val%2 == 0; });
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;These matchers allow any predicate to be applied to element of a collection, with the test only passing if all/any of the elements in collection satisfy the predicate.&lt;/p&gt;

&lt;p&gt;Get version 1.1 &lt;a href="https://github.com/downloads/adscott/jasmine-underscore/jasmine-underscore-v1.1.zip"&gt;here&lt;/a&gt;. Enjoy!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>jasmine-underscore</title>
    <link rel="alternate" href="http://adams.co.tt/blog/2011/06/19/jasmine-underscore/"/>
    <id>http://adams.co.tt/blog/2011/06/19/jasmine-underscore/</id>
    <published>2011-06-19</published>
    <updated>2011-06-19</updated>
    <author>
      <name></name>
    </author>
    <content type="html">&lt;p&gt;I love &lt;a href="http://pivotal.github.com/jasmine/"&gt;Jasmine&lt;/a&gt;. It&amp;rsquo;s a fantastic testing framework with a good set of extensions (e.g. &lt;a href="https://github.com/velesin/jasmine-jquery/"&gt;jasmine-jquery&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;I also love &lt;a href="http://documentcloud.github.com/underscore/"&gt;Underscore&lt;/a&gt;. It has a number of utility functions that help improve the readability of my code and dramatically reduces the code duplication associated with managing collections in JavaScript.&lt;/p&gt;

&lt;p&gt;Recently I&amp;rsquo;ve been using Jasmine and Underscore together in tests. Underscore provides a number of functions to interrogate the state of an object, and I find I often use these in my Jasmine specs:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;it('should return today as a date', function () {
  var cal = new AwesomeCalendar();
  expect(_(cal.today()).isDate()).toBeTruthy();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is a useful test, however it doesn&amp;rsquo;t read quite as nicely as it could. There is a lot of noise, and the failure reporting won&amp;rsquo;t be particularly informative. With that in mind I created &lt;a href="https://github.com/adscott/jasmine-underscore"&gt;jasmine-underscore&lt;/a&gt;. This extension creates matchers for Jasmine based on the Underscore&amp;rsquo;s object functions. Any function that Underscore provides that has the &amp;lsquo;is&amp;rsquo; prefix can be used as a matcher. For example, the test above becomes:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;it('should return today as a date', function () {
  var cal = new AwesomeCalendar();
  expect(cal.today()).toBeDate();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is not only more readable, it also produces better failure messages. The first spec&amp;rsquo;s error message would be &lt;code&gt;Expected false to be truthy&lt;/code&gt;. Now the failure will read &lt;code&gt;Expected null to be date&lt;/code&gt; if today unexpectedly returns null.&lt;/p&gt;

&lt;h2&gt;using&lt;/h2&gt;

&lt;p&gt;Another situation I found myself using Underscore with Jasmine a great deal is the generation of examples for specs:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;_([
    [1, 2, 3], 
    [2, 3, 5]
  ]).each(function (vals) {

  var first = vals[0];
  var second = vals[1];
  var total = vals[2];

  it('should sum ' + first + ' and ' + second, function () {
      expect(sum(first, second)).toEqual(total);
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Again, this somewhat noisy, and could read somewhat better. The jasmine-underscore extension provides the &lt;code&gt;using&lt;/code&gt; function. It is backed by Underscore and helps generate test examples in a meaningful way:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;using([1, 2, 3], [2, 3, 5], function (first, second, total) {
  it('should sum ' + first + ' and ' + second, function () {
      expect(sum(first, second)).toEqual(total);
  });
});
&lt;/code&gt;&lt;/pre&gt;
</content>
  </entry>
</feed>
