<?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>Chris Pratt // Metaphors Be With You &#187; rspec</title>
	<atom:link href="http://www.chrisdpratt.com/tag/rspec/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.chrisdpratt.com</link>
	<description>Random thoughts on random topics. What? You wanted predictability?</description>
	<lastBuildDate>Fri, 31 Jul 2009 14:58:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Meet RSpec: Your Friendly, Neighborhood BDD Framework</title>
		<link>http://www.chrisdpratt.com/2007/09/23/meet-rspec-your-friendly-neighborhood-bdd-framework/</link>
		<comments>http://www.chrisdpratt.com/2007/09/23/meet-rspec-your-friendly-neighborhood-bdd-framework/#comments</comments>
		<pubDate>Mon, 24 Sep 2007 01:05:10 +0000</pubDate>
		<dc:creator>chrisdpratt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://chrisdpratt.webfactional.com/?p=26</guid>
		<description><![CDATA[
Hope Ol&#8217; Stan Lee doesn&#8217;t have that &#8220;friendly, neighborhood&#8221; bit trademarked&#8230; ah well&#8230; life is to short to worry about such things.
Welcome to your whirlwind tour of RSpec, a Behavior Driven Development framework for Ruby and Ruby on Rails. I debated with myself on how best to start this series of tutorials, and eventually decided [...]]]></description>
			<content:encoded><![CDATA[<p><!-- ckey="092FA59D" --><br />
Hope Ol&#8217; Stan Lee doesn&#8217;t have that &#8220;friendly, neighborhood&#8221; bit trademarked&#8230; ah well&#8230; life is to short to worry about such things.</p>
<p>Welcome to your whirlwind tour of RSpec, a Behavior Driven Development framework for Ruby and Ruby on Rails. I debated with myself on how best to start this series of tutorials, and eventually decided to take a breadth-first approach, i.e. dump it all in your lap and break it down from there. So this first post we&#8217;ll cover just what RSpec is and what it can do for you and your code.</p>
<p>Since RSpec is simply an implementation of Behavior Driven Development, you&#8217;d be best served to read my previous post <a href="http://chrisdpratt.com/2007/09/03/an-introduction-to-behavior-driven-development/">An Introduction to Behavior Driven Development</a> first, if you don&#8217;t know what that is.</p>
<p>Getting back to RSpec. RSpec, in addition to being an implementation of BDD, is more specifically, a DSL, or Domain Specific Language. Which, itself, is a fancy way of saying that it is a programming language designed for a specific purpose or task: namely, the task of specifying the behavior of an application. The programming language part may be a bit obtuse in meaning; it&#8217;s not another C or Java or VisualBasic. However, it does introduce a new &#8216;language&#8217; in Ruby that is specific to defining application behavior.</p>
<p>Now that you know what it is, let&#8217;s get to the fun stuff and see what it can do.</p>
<p>I&#8217;ve already stated that RSpec&#8217;s purpose is to specify the behavior of an application. More to the point, RSpec provides a way for us to set down in stone exactly what our application <em>should</em> do and how it <em>should</em> behave. Notice the emphasis on &#8217;should&#8217;. &#8216;Should&#8217; is a crucial focal point of BDD and it&#8217;s how RSpec makes its assertions.</p>
<p>For example, if I wanted to specify that a post should have a title, I would write something a little like this:</p>
<p>describe Post do<br />
before(:each) do<br />
@post = Post.new<br />
end</p>
<p>it &#8220;should have a title&#8221; do<br />
@post.title.should_not be_blank<br />
end<br />
end</p>
<p>There&#8217;s actually quite alot going on there, so let&#8217;s break it down.</p>
<p>We start with creating a <code>describe</code> block, which encapsulates our specification. The part right after <code>describe</code> is the name of the class we are describing, in this case: <code>Post</code>.</p>
<p>Then, we have a <code>before(:each)</code> block, which acts as a setup method or initializer for the specification. The <code>:each</code> specifies that we want the following instructions performed for every example. Inside this block, we store a new instance of the <code>Post</code> class in the instance variable, <code>@post</code>.</p>
<p>Next, we add an <code>it</code> block, which tells RSpec we&#8217;re starting an example. The example, itself, is everything between the <code>do</code> and <code>end</code>. The string after <code>it</code>, &#8220;should have a title&#8221;, is purely informational. You could even leave it out. In which case, RSpec would automatically generate a description of the example based on what is inside. In this case, that would end up being, &#8220;title should not be blank&#8221;.</p>
<p>Inside the <code>it</code> block, we have the real meat of our example.  We access the title attribute and specify that it <code>should_not be_blank</code>.</p>
<p><code>should_not</code> is a method RSpec extends the superclass <code>Object</code> with; <code>should_not</code> and it&#8217;s positive version <code>should</code> will both return true or false, resulting in either a pass or fail, respectively, for our example.</p>
<p><code>be_blank</code> is bit of a different beast. It is not a method itself, but rather signals to RSpec to call the <code>blank?</code> method on our title attribute. <code>blank?</code>, by the way, is an innate method (exists out of the box) that returns true if the variable in question is either an empty string or <code>nil</code>.</p>
<p>Every example you code in RSpec will have at least one statement with <code>should</code> or <code>should_not</code>. The <code>be_<em>whatever</em></code> part is just one of many different possible arguments (matchers) for these two methods. Let&#8217;s look at the full list:</p>
<h2>Eql and Equal</h2>
<p>RSpec has two levels of equality checks. First, you can use <code>eql</code> or the standard equality operator <code>==</code>. For example:</p>
<p><code>"this string".should eql "this string"</code>   or   <code>"this string".should == "this string"</code></p>
<p>Second, you can check for equality at the object level with <code>equal</code> or the &#8220;three equals&#8221; operator <code>===</code>. For example:</p>
<p><code>25.should equal 25</code>   or   <code>25.should === 25</code></p>
<p>However, note:</p>
<p><code>"this string".should equal "this string"</code>   and   <code>"this string".should === "this string"</code></p>
<p>Both of these will actually evaluate to false. To understand why you could run script/console and try the following:</p>
<p><code> </code></p>
<pre>
$ ruby script/console
Loading development environment.
&gt;&gt; "this string".object_id
=&gt; 106148390
&gt;&gt; "this string".object_id
=&gt; 106145680</pre>
<p>Even though it&#8217;s the same string, each instance has a unique object id. The example using 25 works because of the following:</p>
<p><code> </code></p>
<pre>
$ ruby script/console
Loading development environment.
&gt;&gt; 25.object_id
=&gt; 51
&gt;&gt; 25.object_id
=&gt; 51</pre>
<p>The technical explanation is that 25 is instantiated as a Fixnum, so every instance of 25 afterwards is actually a reference to the first instance of 25. However, strings and most other objects in Ruby receive unique ids for every instance.</p>
<h2>Have</h2>
<p>Quite often, you&#8217;ll want to ensure that a collection has at least a certain number of items: ensuring there are no validation errors on a particular field in a form, for example. RSpec provides an easy way to specify the behavior you want with <code>have</code>. For example:</p>
<p><code>day.should have(24).hours</code></p>
<p>It should be noted that in order for this to work, <code>day</code> would have to own a collection, <code>hours</code>, with 24 items.  This is really just shorthand for the following:</p>
<p><code>day.hours.should have(24).items</code></p>
<p>RSpec provides some &#8220;sugar&#8221; with <code>have</code> so that both of the following will work without any further implementation:</p>
<p><code>[1, 2, 3].should have(3).items</code>   and   <code>"fun".should have(3).characters</code></p>
<p>Additionally, some things in RSpec exist simply for the purpose of readability. One example of this is <code>have_exactly</code>, which is just an alias for <code>have</code>. So, the code above could be be rewritten as:</p>
<p><code>day.should have_exactly(24).hours</code></p>
<p>Either way works; it&#8217;s simply a matter of preference.</p>
<p>Often, you won&#8217;t be entirely sure about how many items you should have, but you will know a minimum or maximum. Once again, RSpec comes through with <code>have_at_least</code> and <code>have_at_most</code>. For example:</p>
<p><code>blog.should have_at_least(1).post</code>   or   <code>question.should have_at_most(4).answer_choices</code></p>
<h2>Include</h2>
<p>Closely related to <code>have</code>, we have <code>include</code>. Whereas, <code>have</code> concerns itself with the number of items in a collection, <code>include</code> concerns itself with the collection&#8217;s values. For example:</p>
<p><code>{ :str1 =&gt; 'first string', :str2 =&gt; 'second string' }.should include('second string')</code></p>
<p>The above would pass because the value &#8217;second string&#8217; exists in the the collection. You can even specify multiple values as such:</p>
<p><code>[1, 2, 3, 4, 5].should include(1, 3, 5)</code></p>
<p>However, when you specify multiple values for the <code>include</code> all of those values must be present in order for the example to pass. The following would fail, since 6 is not present in the array:</p>
<p><code>[1, 2, 3, 4, 5].should include(2, 4, 6)</code></p>
<h2>Match</h2>
<p>RSpec provides the ability to match based on regular expressions through the <code>match</code> matcher. Like <code>eql</code> and <code>equal</code> before, there&#8217;s you can also the use the operator equivalent, <code>=~</code>. For example:</p>
<p><code>"sample string".should match(/sample/)</code>   or   <code>"sample string".should =~ /sample/</code></p>
<h2>Predicates</h2>
<p>The previous matchers have all been methods specific to RSpec. The true power of RSpec comes from letting you apply its behavior-specific language to your own methods. It does this via various prefixes. We encountered one of these earlier with <code>be_blank</code>. As we saw, the actual method being called is <code>blank?</code>, but by prefixing it with <code>be_</code> we can not only instruct RSpec to call this method, but also maintain nicer readability. Here are some other common uses of the <code>be_</code> prefix:</p>
<p><code>be_true</code><br />
<code>be_false</code><br />
<code>be_nil</code><br />
<code>be_blank</code><br />
<code>be_empty</code><br />
<code>be_an_instance_of(<em>Class</em>)</code><br />
<code>be_a_kind_of(<em>Class</em>)</code><br />
<code>be_close(<em>expected</em>, <em>delta</em>)</code></p>
<p>Each of the above rely on built in Ruby methods, but you can also apply this to any other predicate. What is a predicate? Predicates are methods that end with ?&#8217;s and return true or false. It could be one built into Ruby or Ruby on Rails, or it could be one of your own creation. Consider the following:</p>
<p># method defined in User class<br />
def admin?<br />
self.role == &#8216;admin&#8217;<br />
end</p>
<p>&#8230;</p>
<p># example from User spec<br />
@user.should be_admin # will pass if @user.role == &#8216;admin&#8217;</p>
<p>Also, notice the two matchers above, <code>be_a_kind_of</code> and <code>be_an_instance_of</code>. These respectively call the methods <code>kind_of?</code> and <code>instance_of?</code>. The prefixes <code>be_a_</code> and <code>be_an_</code> are aliases of <code>be_</code>, provided for better readability.</p>
<p>In addition to the <code>be_</code> prefix, RSpec also provides the <code>have_</code> prefix. Note, however, that this is not the same as the <code>have</code> matcher mentioned above. It&#8217;s also not a prefix in the truest since, but the best way to explain is with an example:</p>
<p><code>{:fun =&gt; 1, :times =&gt; 2 }.should have_key(:fun)</code></p>
<p>In this example <code>have_key</code> signals RSpec to call the predicate <code>has_key?</code> built into Ruby. This will work with any predicate starting with <code>has_</code> as well. For example:</p>
<p># defined inside a Chicken class<br />
def has_feathers?<br />
self.feathers &gt; 0<br />
end</p>
<p>&#8230;</p>
<p># example inside the Chicken spec<br />
@plucked_chicken.should_not have_feathers # passes if @plucked_chicken.feathers == 0</p>
<h2>Is that it?</h2>
<p>There&#8217;s a few other matchers that I have intentionally not mentioned yet: only because they are a little more abstract in nature. We&#8217;ll eventually get to all those as we continue through this series.</p>
<p>The next article up will detail the process of spec&#8217;ing a model. I haven&#8217;t completely decided on an example application yet, but I have a couple ideas.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisdpratt.com/2007/09/23/meet-rspec-your-friendly-neighborhood-bdd-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby on Rails Development in Windows via Cygwin</title>
		<link>http://www.chrisdpratt.com/2007/09/17/ruby-on-rails-development-in-windows-via-cygwin/</link>
		<comments>http://www.chrisdpratt.com/2007/09/17/ruby-on-rails-development-in-windows-via-cygwin/#comments</comments>
		<pubDate>Mon, 17 Sep 2007 05:38:09 +0000</pubDate>
		<dc:creator>chrisdpratt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cygwin]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://chrisdpratt.webfactional.com/?p=24</guid>
		<description><![CDATA[Such a love/hate relationship I have with Linux. It has everything but a great editor (Please no flames from the vi and emacs crowd&#8230; those are great editors if you know how to use them properly, but I&#8217;d prefer to spend my time learning Ruby and Rails rather learning how to use a text editor).
Windows, [...]]]></description>
			<content:encoded><![CDATA[<p>Such a love/hate relationship I have with Linux. It has everything but a great editor (Please no flames from the vi and emacs crowd&#8230; those are great editors if you know how to use them properly, but I&#8217;d prefer to spend my time learning Ruby and Rails rather learning how to use a text editor).</p>
<p>Windows, on the other hand, lacks much in the Rails development arena, but has been graced with a really great text editor: e-Text Editor, a TextMate clone for Windows. That&#8217;s just enough motivation to keep me hacking away at Windows (at least until I get a Mac&#8230; then you&#8217;re all on your own).</p>
<p>So on to the actual point of this article. Via Cygwin, I have been able to setup up an acceptable Rails development environment for myself in Windows, but since Cygwin can be a little tricky to setup properly, I decided to get some community service hours in by providing a tutorial.</p>
<p>Without further ado (cue drum-roll and general fan-fare)&#8230;</p>
<h2>Preparation</h2>
<p>I had various things such as Ruby and MySQL already set up on my computer before installing Cygwin. The Windows version of Ruby will have to go the way of the dinosaurs once we bring in Cygwin, but it does play well with the Windows version of MySQL. However, I&#8217;m not sure if that would still be the case if setup after Cygwin (it probably would, but why take any chances?), so our first step will be to setup MySQL.</p>
<h3>Install MySQL for Windows</h3>
<p>There&#8217;s a few million tutorials on how to do this, so I&#8217;m going to cover the bare minimum. If you run into any problems, go make friends with Google.</p>
<p>First, go <a href="http://dev.mysql.com/downloads/mysql/5.0.html#win32">download the current MySQL binary</a>. At the time of this writing, it&#8217;s 5.0.45. You can download either the Essentials or the regular setup executable. I normally go with the regular setup since space is not an issue for me, but I&#8217;ll leave the choice to you. Run the installer, and follow the on screen prompts. The install process is pretty self-explanatory, but I&#8217;ll cover a bit of minutiae.</p>
<p>You can pretty much use the default settings throughout the install. I normally like to keep all my server apps (apache, mysql, etc) in a common directory for ease. If you&#8217;d like to do the same, you can change the install directory when you have the option. After the install finishes, you&#8217;ll be asked if you&#8217;d like to go ahead and configure MySQL. Yes you do, so proceed with that. Choose the development options throughout (the explanatory paragraphs will mention that this setting or that setting is better for either production or development). When you&#8217;re asked if you&#8217;d like MySQL to run as a Windows service, tell it yes. When you&#8217;re prompted to setup accounts, make sure you disable the anonymous account. You can leave the root password blank, as long as you also turn off remote access. The default Rails database config has blank root passwords, so it&#8217;ll save you from having to customize the config every time you start a new app, and it&#8217;s just easier to not have to type in a password every time you want to do something with MySQL. The rest of this tutorial will assume the password is blank, though I&#8217;ll provide little pointers here and there in case you did set a password.</p>
<h2>Install Cygwin</h2>
<p>With that out of the way, we can move on to Cygwin. You can grab the setup at the <a href="http://www.cygwin.com/">Cygwin website</a>. Look for the &#8220;Install or Update Now!&#8221; link around the center of the home page. The setup file is a small download, but go ahead and get a pot of coffee ready for brewing, because the rest will be downloaded during install.</p>
<p>Run the setup program, and click next. You&#8217;re going to want to stick with the default here (Install from Internet), so click next again. The root directory should default to &#8216;C:\cygwin&#8217;. If not, it&#8217;ll make your life a little easier if you change it to that. Leave the two recommended settings checked, and click next again. Now, you&#8217;re going to be prompted for a Local Package Directory. I forget what this defaults to, but I like to set it to &#8216;C:\cygwin\install&#8217; just to keep everything together. Click next again, and you&#8217;ll be prompted for your connection settings. For most purposes you should be fine with the default of &#8220;Direct Connection&#8221;. If that doesn&#8217;t work, and you&#8217;re unsure of what your settings should be, you can try &#8220;Use IE5 Settings&#8221;. If you know you&#8217;re connecting through a proxy, what are you bothering me for? Just enter your information there.</p>
<p>Time for a new paragraph for this rather tedious install process, but we&#8217;re getting close to the end. After clicking next, you&#8217;ll be prompted to choose a download site. I always feel a little bit like I&#8217;m in Vegas at this point in the install, because it&#8217;s basically just a gamble as to which one to pick. FTP is more efficient for file transfer than HTTP, so that weeds it down a bit. Other than that, you can either pick one at random or try to discern one that might be close to you. There&#8217;s alot of college servers there, so if you recognize a college that close to your area, go ahead and pick that one.</p>
<p>Okay, I devoted way more attention than was necessary to picking a mirror. Once you&#8217;ve rolled the dice, click next. The installer is  now going to download a list of available packages and then it&#8217;ll present you with a very cryptic screen. Fortunately, we&#8217;re going to stick with the defaults for most of these, but we will have to make one small change. Expand the &#8220;Devel&#8221; category, and then scroll down in the list until you come to the &#8220;ruby&#8221; package. Click where it says &#8220;Skip&#8221; to set it to install. And with that, you can click next again and go have a cup of coffee.</p>
<p>When the install completes. You&#8217;ll be asked if you&#8217;d like to create a Desktop icon or Program Group, give one or both a check, just to simplify your life, and you&#8217;re done.</p>
<h2>Installing RubyGems</h2>
<p>The process from here ought to be familiar for awhile if you&#8217;ve ever done any Rails development before. However, we do have a few gotchas to take a look at.</p>
<p>First, <a href="http://rubyforge.org/frs/?group_id=126&amp;release_id=11889">download the rubygems archive</a>, which is at version 0.9.4 at the time of this writing. Save it to your desktop to make things easy on yourself. Now, we need to open up a Cygwin command prompt, using one of our handy, dandy shortcuts (either on your desktop or in your start menu&#8230; you did remember to do that right?). In the command prompt, you&#8217;ll already be in your home directory (i.e. C:\Documents and Settings\[your name]\) which is short-handed as &#8216;~&#8217; (Tip: you can run &#8216;cd ~&#8217; at any point to return to your home directory). We need to get to the RubyGems archive we downloaded, so if you followed instructions that&#8217;ll mean running &#8216;cd Desktop&#8217;. Now, run the following command (remembering to change the version to whatever is appropriate):</p>
<p><code> </code></p>
<pre>$ tar -pxzf rubygems-0.9.4.tgz</pre>
<p>After that finishes it&#8217;s work change into the directory that was created:</p>
<p><code> </code></p>
<pre>$ cd rubygems-0.9.4</pre>
<p>And run the setup.rb program:</p>
<p><code> </code></p>
<pre>$ ruby setup.rb</pre>
<p><strong>Note:</strong></p>
<p>At some point either by now or in the next few steps, you might come across a cryptic error resembling:</p>
<p><code> </code></p>
<pre>ruby: no such file to load -- ubygems (LoadError)</pre>
<p>If or when this occurs, you can simply run the following command, and you&#8217;ll be good to proceed:</p>
<p><code> </code></p>
<pre>$ unset RUBYOPT</pre>
<p><strong>Now back to our regularly scheduled tutorial&#8230;</strong></p>
<h2>Install Rails</h2>
<p>Normally, this is a easy as:</p>
<p><code> </code></p>
<pre>$ gem install rails --include-dependencies</pre>
<p>But I have recently noticed, both in Linux and Cygwin that this will return a NoSuchGem error. (Confirmation?). Go ahead and try it the tried and true way first, but if that doesn&#8217;t work, continue on.</p>
<p>Go ahead now and <a href="http://rubyforge.org/frs/?group_id=307">download the rails gem</a>, which is at 1.2.3 at the time of this writing. Save it to your desktop, like before. Back in your Cygwin command prompt change back to your desktop (&#8217;cd ..&#8217;), and run the following command to take care of the dependencies:</p>
<p><code> </code></p>
<pre>$ gem install rake activerecord actionpack actionmailer actionwebservice</pre>
<p>You&#8217;ll be prompted at points to confirm the download of additional dependencies. Of course, you&#8217;ll want to allow those. Grab another cup of coffee, and when it finishes up, run:</p>
<p><code> </code></p>
<pre>$ gem install rails-1.2.3.gem</pre>
<p>With that, we now have Rails up and running.</p>
<h2>Getting Autotest</h2>
<p>Autotest is a very nice feature that will run both your tests and specs automatically based on file changes. It&#8217;s actually just a part of a larger suite of testing tools, though, available through the ZenTest gem. Let&#8217;s go ahead and get that:</p>
<p><code> </code></p>
<pre>$ gem install zentest</pre>
<h2>Creating a Rails App</h2>
<p>Now that we&#8217;ve got all the boring stuff out of the way, let&#8217;s get to the interesting part, setting up a Rails app and bootstrapping it for Behavior Driven Development via the RSpec framework. First, change to whatever directory you want to store your Rails apps in (I like to simply use my home directory) and then run:</p>
<p><code> </code></p>
<pre>$ rails myapp</pre>
<p>(Where &#8216;myapp&#8217; is whatever you want to name your app). Then change into the new app&#8217;s directory, via:</p>
<p><code> </code></p>
<pre>$ cd myapp</pre>
<p>This is where all the fun will happen. But I&#8217;ve gotten a little ahead of myself. Before we continue, we need to take care of a few little things. First, we&#8217;re going to need both a test and development database for our new app. We can take care of that easily with:</p>
<p><code> </code></p>
<pre>$ mysqladmin -uroot create myapp_development</pre>
<p>and</p>
<p><code> </code></p>
<pre>$ mysqladmin -uroot create myapp_test</pre>
<p>Now we need to make a slight change to the database.yml config file for our app, due to some Cygwin/Windows compatibility issues. Open the database.yml file (found in the config folder of your app&#8217;s directory) in your favorite text editor or wordpad (notepad would not be a good choice due to the Unix format line-endings). Once there, replace every occurrence of &#8216;localhost&#8217; with &#8216;127.0.0.1&#8242; (fixes a socket problem with MySQL).</p>
<h2>Install the RSpec and Spec:Rails Plugins</h2>
<p>There, that&#8217;s taken care of. Let&#8217;s bootstrap our app with RSpec. (<em>Oddly enough though, we can&#8217;t seem to do this in Cygwin. I ran into a very weird issue trying. The plugins appear as if they are installing normally, but afterwards, nothing shows up in the vendors/plugins directory of the app. Odder still, if you try to install the plugins again, it will actually error out, saying that the directories already exist. The only way I could get the plugins installed was to backdoor it through a standard Windows command prompt, but that requires keeping a separate version of Ruby on the Windows side. (Confirmation?) Unfortunately, I have not been able to find another way, so for the meantime, I will go ahead and still put the process here. However, be advised that it may not work.</em>)</p>
<p><code> </code></p>
<pre>$ ruby script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec</pre>
<p>and when that finishes:</p>
<p><code> </code></p>
<pre>$ ruby script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec_on_rails</pre>
<p><strong>Note:</strong></p>
<p>If you&#8217;re feeling adventurous, you can download the trunk versions of these plugins instead. As of this writing, this would give you access to the new Story Runner functionality of RSpec, that has not yet been released. Instead of the above commands run:</p>
<p><code> </code></p>
<pre>$ ruby script/plugin install svn://rubyforge.org/var/svn/rspec/trunk/rspec</pre>
<p>and then:</p>
<p><code> </code></p>
<pre>$ ruby script/plugin install svn://rubyforge.org/var/svn/rspec/trunk/rspec_on_rails</pre>
<p><strong>Now back to our tutorial, already in progress&#8230;</strong></p>
<p>Whichever route you choose above, the next step is to generate the standard RSpec folders and files:</p>
<p><code> </code></p>
<pre>$ ruby script/generate rspec</pre>
<h3>Generating Some Stuff to Play With</h3>
<p>And that&#8217;s all there is to it. But for the sake of instruction let&#8217;s generate a sample controller and model. This will let us run autotest and see how the whole process went. I&#8217;ll be pretending that this app is going to be a blog, but you can go ahead and create something more meaningful here if you want.</p>
<p>The model:<br />
<code> </code></p>
<pre>$ ruby script/generate rspec_model post</pre>
<p>The controller:<br />
<code> </code></p>
<pre>$ ruby script/generate rspec_controller blog</pre>
<p>After these two complete, open the 001_create_posts.rb migration file that was created in the apps db/migrate directory. It will initially look like this:</p>
<p>class CreatePosts &lt; ActiveRecord::Migration<br />
def self.up<br />
create_table :posts do |t|<br />
end<br />
end</p>
<p>def self.down<br />
drop_table :posts<br />
end<br />
end</p>
<p>Modify it to look like this:</p>
<p>class CreatePosts &lt; ActiveRecord::Migration<br />
def self.up<br />
create_table :posts do |t|<br />
t.column :title, :string<br />
t.column :content, :string<br />
t.column :created_at, :datetime<br />
end<br />
end</p>
<p>def self.down<br />
drop_table :posts<br />
end<br />
end</p>
<p>Save and close. Then go back to your Cygwin command prompt and run:</p>
<p><code> </code></p>
<pre>$ rake db:migrate</pre>
<p>followed by:</p>
<p><code> </code></p>
<pre>$ rake db:test:clone</pre>
<p>With that done, we can run autotest and get a glimpse of the rest of our lives in development</p>
<p><code> </code></p>
<pre>
$ autotest
loading autotest/rails_rspec
/usr/bin/ruby -S script/spec -O spec/spec.opts  spec/helpers/blog_helper_spec.rb
 spec/controllers/blog_controller_spec.rb spec/models/post_spec.rb
...

Finished in 0.389 seconds

<span style="color: #33ff33">3 examples, 0 failures</span></pre>
<p>Those three examples are default examples generated for us by RSpec.</p>
<h2>Finally&#8230;</h2>
<p>Take a breather now. You&#8217;ve done alot of work, and accomplished quite alot too. I hope this helps those of you who are stuck programming in a Windows environment.</p>
<p>As always, if something is unclear, feel free to ask for further explanation. I do my best to try and write to the lowest common denominator, but I don&#8217;t always succeed. Thanks for reading. Now get out there and do some coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisdpratt.com/2007/09/17/ruby-on-rails-development-in-windows-via-cygwin/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Rails, RSpec, Autotest, Redgreen, and Snarl: Reasons I don&#8217;t like Windows</title>
		<link>http://www.chrisdpratt.com/2007/09/04/rails-rspec-autotest-redgreen-and-snarl-reasons-i-dont-like-windows/</link>
		<comments>http://www.chrisdpratt.com/2007/09/04/rails-rspec-autotest-redgreen-and-snarl-reasons-i-dont-like-windows/#comments</comments>
		<pubDate>Tue, 04 Sep 2007 07:55:23 +0000</pubDate>
		<dc:creator>chrisdpratt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[autotest]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[redgreen]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[snarl]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://chrisdpratt.webfactional.com/?p=21</guid>
		<description><![CDATA[In preparation for an article I&#8217;m writing on RSpec, I decided to revisit setting up my preferred RSpec development environment in Windows. I was ever so quickly reminded of why I made the switch to Linux in the first place.
The Background
Let me get everyone caught up to speed. RSpec is of course a Behavior Driven [...]]]></description>
			<content:encoded><![CDATA[<p>In preparation for an article I&#8217;m writing on RSpec, I decided to revisit setting up my preferred RSpec development environment in Windows. I was ever so quickly reminded of why I made the switch to Linux in the first place.</p>
<h2>The Background</h2>
<p>Let me get everyone caught up to speed. RSpec is of course a Behavior Driven Development framework for Ruby and Ruby on Rails. The Behavior Driven Development process itself involves writing a specification (test), running that specification (which will initially fail), writing the functionality to satisfy that specification, and finally running the specification again which should then pass. This process continues until you&#8217;ve implemented all the functionality in the application you are developing.</p>
<p>Now, if you run all the specifications manually, this can get really bothersome very quickly. This is particularly aggravated in a Windows environment, since all the common Rails tasks (rakes, generations, migrations, etc.) seem to take twice as long as they do in a Linux or OSX environment. Even under Linux or OSX, though, the frequency of running specs tends to slow down the production cycle incredibly when run manually.</p>
<p>Thankfully, there&#8217;s ZenTest and more specifically the autotest functionality of ZenTest. Autotest watches for file changes and then automatically runs any specs or tests that have changed. Combine this with the color-coding provided by Redgreen and a notification system (Growl, KDENotify, Libnotify) and you have a very effective Behavior Driven Development environment.</p>
<h2>The Specs</h2>
<p>There is strong indication that all of the issues I have noticed are version dependent (i.e. broken in newer versions), so it makes sense, then, to provide the specs of the various gems and plugins I&#8217;m using for these tests:</p>
<ul>
<li>Windows XP SP2</li>
<li>Ruby 1.8.6</li>
<li>Rails 1.2.3</li>
<li>ZenTest 3.6.1</li>
<li>RSpec 1.0.8</li>
<li>Spec:Rails 1.0.8</li>
<li>Redgreen 1.2.2</li>
<li>Ruby-Snarl 0.0.8</li>
<li>Snarl 1.6</li>
</ul>
<h2>The Issues</h2>
<p>But then there&#8217;s Windows. On the surface, it looks as though the same should be possible here. We can develop Rails apps in Windows. RSpec, Autotest, and Redgreen are operating system agnostic. There&#8217;s even a fairly mature notification system for Windows in Snarl. So what&#8217;s the problem? Well, when you put all of these together, it explodes.</p>
<p>I did a good bit of trial and error debugging tonight trying to pin down the exact problem, and while I was ultimately unsuccessful, I was able to notice certain trends which, to a more apt Rubyist, may spark an idea for a solution. So, for posterity, I want to post my findings in the hope that someone can figure out what is wrong.</p>
<h3>Autotest and Redgreen</h3>
<p>A great deal of work has been done to integrate RSpec and Autotest, and in Linux and OSX, the two play very nicely together with little to no configuration. In Windows, however, Autotest initially hangs when trying to read the specs. The only way I could find to get Autotest to actually run the specs through, was to remove everything from the spec.opts file in the spec directory of the app in question.</p>
<p>Unfortunately one of the lines in the spec.opts file is the &#8220;&#8211;colour&#8221; option which sets up the color-coding of passes and failures. I did quite a bit of research online, and found many articles which tell you that you must install the redgreen gem in order to get color-coding on your tests. However, this seems to be outdated information since both Autotest and RSpec seem to come with this functionality out of the box, now (assuming you have installed the win32console gem to make it work in Windows). Indeed, even without the redgreen gem installed, if I simply &#8216;rake spec&#8217; the color-coding is there.</p>
<p>So, after realizing that I could not rely on this out of the box functionality in Windows, I tried the alternate route of installing the redgreen gem and then requiring it in a &#8216;.autotest&#8217; file in my RAILSROOT (require &#8216;redgreen/autotest&#8217; along with require &#8216;Win32/Console/ANSI&#8217;). However, this resulted in autotest erroring-out on load complaining of an &#8216;Invalid Option: -O&#8217;.</p>
<p>I then realized that Autotest has a redgreen plugin of its own, which can be reference as flip-flop of the previous require for the redgreen gem (i.e. require &#8216;autotest/redgreen&#8217;). So I uninstalled the redgreen gem, changed the require in .autotest, and ran autotest again. This time it loads fine, but without color-coding still. Finally, just to be thorough, I reinstalled the redgreen gem in the hopes that it still somehow provided some functionality for the autotest plugin of the same name. However, this two proved fruitless. Autotest still ran successfully, but still without color-coding.</p>
<h3>Autotest and Snarl</h3>
<p>At this point, I gave up on having nice color-coded test results. Snarl, I thought, can be setup to provide all the visual indication necessary, so I began to work on the Snarl side of things. I already had Snarl installed, so I went ahead and installed the ruby-snarl gem which sets up the connectivity. Then, I opened my .autotest file once more and added the require for snarl (require &#8216;autotest/snarl&#8217;). Time to test it out, so I went to my command line, ran autotest once more, only to see Snarl pop up with a notification window saying all tests had passed, while autotest simultaneously informed me that I had two failures. This is pretty much where I stopped because I have no idea where to even begin to troubleshoot Snarl.</p>
<h2>The Summary</h2>
<p>In the end, the only thing I could get to work was plain-jane autotest, which is better than nothing but not by much. Through all the digging I have done online, the only thing I can determine definitively is that at one point it was possible to integrate all of this in Windows. However, this was before both Autotest and RSpec added in their own implementations of Redgreen. The Snarl issue is even stranger, and I&#8217;m not sure if it is a related issue or something entirely separate.</p>
<p>The ironic part is that it appears that it has been so for quite some time, and no one is either using Windows to notice it, or cares enough to fix it if they have noticed it. Which gives even greater weight in my mind to simply abandon Windows for Rails development, preferring Linux or OSX. Obviously, OSX would be the optimum choice, but for those of us still on PCs, Linux is as close as you can get.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisdpratt.com/2007/09/04/rails-rspec-autotest-redgreen-and-snarl-reasons-i-dont-like-windows/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
