Jan 09 2009

RSpec is a hit, who’d o’ thought?

dastels @ 7:14 pm

Well, RSpec is doing well. A couple things of note recently:


Sep 19 2008

Arrrrrrrrrrrspec fer th’ win

dastels @ 2:46 pm

Happy Talk Like a Pirate day!


Jan 16 2008

RSpec + JRuby

dastels @ 6:17 am

My coworker, Paul Zabelin, posted here on some ideas that we’ve been experimenting with using RSpec stories and JRuby.


Jan 15 2008

New and Improved: RSpec 1.1.2

dastels @ 11:48 am

Tonight RSpec-1.1.2 was released. See most of the details on David Chelimsky’s blog.

I take a personal interest in this release as it includes my first active submission to the codebase in some time.

The functionality I added relates to the definition of steps in the new story component. Up until now, you used a string to define a step. For example:

Given "a student named '$name'" do |name|
  #...
end

When "the student is given a grade of $grade" do |grade|
  #...
end

Then "the student should $pass_fail" do |pass_fail|
  #...
end

This would result in stories like the following:

Given a student named 'Mike'
When the student is given a grade of 40
Then the student should fail

Continually talking about “the student” is grating and very un-natural sounding. Sounds downright, bloody legalistic, actually. And a lawyer is the last thing we want to be accused of sounding like… other than maybe Denny Crane.

One approach to this would be to go to something like:

When "he is given a grade of $grade" do |grade|
  #...
end

Then "he should $pass_fail" do |pass_fail|
  #...
end

which would give us:

Given a student named "Mike"
When he is given a grade of 40
Then he should fail

Now, speaking of lawyers, we probably want to make this a little more PC and be able to do this:

Given a student named "Michelle"
When she is given a grade of 60
Then she should pass

We could conceivable create another set of steps for the feminine forms, refactoring to remove the duplication. That might suffice in the simple case, but it’s still rather crude. I’d like to be able to use a regular expression and create steps something like:

When /(he|she) is given a grade of (.*?)/ do |pronoun, grade|
  #...
end

Then /(he|she) should (.*?)/ do |pronoun, pass_fail|
  #...
end

With release 1.1.2, that’s exactly what you can do.

There are a couple things to point out:

  1. Alternatives need to be in a group to limit their scope.
  2. Whatever matches groups such as that (and any others) will be sent into the supplied block as arguments. As such they need to be accommodated by having a block parameter for each of them.
  3. Since this is already a regexp, no internal processing is done to it. With string step names, variables (of the form $<identifier>) are rewritten as (.*?). When using a regexp as the step name where there are variables, we much do this rewriting ourselves.

This new feature provides a new level of flexibility in defining story steps. Have fun with it.


Sep 20 2007

Everyone’s a pirate

dastels @ 12:23 am

Seen on #caboose:

 

courtenay: we uses arrrrrrrspec 


Sep 11 2007

Describing Equivalence Classes in Ruby with RSpec

dastels @ 8:45 pm

Here’s an article I wrote a while ago but didn’t get around to releasing.  Enjoy. 

Describing Equivalence Classes in Ruby with RSpec

Since writing that article, I’ve made good use of the code. Here’s an example of it in action: grouping the cards in a deck into their types. A second method then uses those groups to reassemble the cards into an ordered list.

 

  CARD_TYPES = ['basic land', 'land', 'creature', 'artifact',
                'enchantment', 'sorcery', 'instant', '']

  def maindeck_cards_grouped
    maindeck_cards.equivalence_classes(*CARD_TYPES) do |card, the_type|
      card.magic_card.card_type.downcase.include?(the_type)
    end
  end

  def maindeck_cards_ordered
    cards_by_class = maindeck_cards_grouped
    (CARD_TYPES.inject([]) {|cards, type| cards << cards_by_class[type]}).flatten
  end

Mar 30 2007

RSpec Autotest now a Rails Plugin

dastels @ 3:30 am

RSpec Autotest now a Rails Plugin:

“Posted by Nick Sieger Wed, 15 Nov 2006 15:46:00 GMT

Inspired by a posting on the RSpec list and recent comments stating that my Auto RSpec hack wasn’t working, I’ve bitten the bullet and upgraded to RSpec 0.7.2, and made rspec_autotest a plugin in the process. So, here are the necessary incantations to auto-rspec your project.”

OK, so I’m a bit behind on RSpec developments. But this plugin makes a world of difference when using rspec.

While Nick mentions rspec 0.7.2 in the post, I’m using it with the latest (pre-trunk) 0.9.0 without any issues.

If you use rspec (with rails or not.. see rspec-autotest-for-standalone-projects) you should be using this plugin!


Oct 29 2006

RSpec On Rails

dastels @ 8:58 pm

Nice post by Defiler on RSpec:

RSpec On Rails:

RSpec is mere days away from a new release with greatly improved Rails support.

Since people are currently paying me to write Rails code, rather than plain old standalone Ruby (hint hint), I’ve been waiting for these features before making serious use of RSpec.

As an exercise, I ‘ported’ the acts_as_authenticated controller tests to RSpec. The results were fairly interesting. Subjectively, I find it more readable than the test/unit version. Objectively, one of the test/unit test cases doesn’t get run because there are two methods with the same name.

So far, so good. Anyone who thinks that RSpec is ‘only’ about a different set of terminology should give it a serious try first.

(Via ~:caboose.)


Sep 14 2006

rSpec Bundle for TextMate

dastels @ 2:07 am

I’ve whipped up a simple TextMate bundle for making rSpec a bit nicer to work with. You can download it here.

You might need to unzip the file (Safari does automatically), and put it in ~/Library/ApplicationSupport/TextMate/Bundles. Note you might have to create the Bundles folder.

At the moment it tweaks the ruby rules for coloring to colour context, specify, setup, and teardown as keywords. It also adds snippets for the same four: context<tab>, specify<tab>, setup<tab>, and teardown<tab>.


Sep 09 2006

rSpec Cheat Sheet

dastels @ 8:20 pm

I’ve made up a cheat sheet for rSpec. It’s checked into the rSpec repository on rubyforge. You can grab it directly here.

Tweaks will be made as rSpec moves forward.

Update: I should mention, it’s 2 pages with a cmd line reference reference on the second page. Works great printed two sided.

SafariScreenSnapz002.png