[Ironruby-core] RSpec Test Format

Jim Deville jdeville at microsoft.com
Tue Jul 7 02:52:42 EDT 2009


I would write it as follows:
###FILE spec_helper.rb####
require 'rubygems'
require 'spec'
def be_prime_for(expected)
  simple_matcher("a prime number") {|given| given.is_prime(expected)}
end

####spec file###
require 'path/to/spec_helper'
require File.dirname(__FILE__) + '/bin/Debug/BusinessObjects.dll'
include BusinessObjects


describe PrimeService do
  before(:each) do
    @prime_service = PrimeService.new
  end

  it "should be prime for 1" do
    @prime_service.should be_prime_for 1
  end

  it "should be prime for 2" do
    @prime_service.should be_prime_for 2
  end

  it "should raise an error for invalid input" do
    lambda {@prime_service.is_prime(-1)}.should raise_error
  end

  it "should be 'true' for prime numbers" do
    [3,23,5,7,11,13,19].each {|x| @prime_service.should be_prime_for x}
  end

  it "should be 'false' for non-prime numbers" do
    [4,6,9,12,21].each {|x| @prime_service.should_not be_prime_for x}
  end
end

Notable points:
a) 2 files, you can add more matchers and other common setup to spec_helper.rb and require it in other spec files.
b) use before(:each) for test setup. It ensures isolation
c) no need for multiple contexts in this case.
d) custom matchers increase the readability

Hope that helps.


JD

...there is no try

> -----Original Message-----
> From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-
> bounces at rubyforge.org] On Behalf Of Mohammad Azam
> Sent: Monday, July 06, 2009 7:11 PM
> To: ironruby-core at rubyforge.org
> Subject: [Ironruby-core] RSpec Test Format
>
> I have done BDD in C# and my question is that how Spec in IronRuby makes it
> easier. Here is what my test looks like:
>
> require 'rubygems'
> require 'spec'
>
> require File.dirname(__FILE__) + '/bin/Debug/BusinessObjects.dll'
>
> include BusinessObjects
>
> primeService = PrimeService.new
>
> describe PrimeService,"when 1 is passed" do
>
> it "should return true" do
>
> primeService.IsPrime(1).should == true
>
> end
>
> end
>
> describe PrimeService, "when 2 is passed" do
>
> it "should return true" do
>
> primeService.IsPrime(2).should == true
>
> end
>
> end
>
> describe PrimeService, "when a number less then zero is passed" do
>
> it "should throw an error for invalid input exception" do
>
> lambda {primeService.IsPrime(-1)}.should raise_error
>
> end
>
> end
>
> describe PrimeService, "when a prime number is passed" do
>
> it "should return a true value" do
>
> prime_numbers = [3,23,5,7,11,13,19,17,29]
>
> prime_numbers.each{|x| primeService.IsPrime(x).should == true}
>
> end
>
> end
>
>
> Is this the normal way of writing unit test BDD style using Spec and IronRuby
> or am I missing something?
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Ironruby-core mailing list
> Ironruby-core at rubyforge.org
> http://rubyforge.org/mailman/listinfo/ironruby-core



More information about the Ironruby-core mailing list