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?

Mohammad A. wrote:

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:

Is this the normal way of writing unit test BDD style using Spec and
IronRuby or am I missing something?

I’m definitely no BDD expert, but I’ll try to take a stab at it.
Apologies if I put my foot in my mouth.
People actually using BDD full-time, please chime in and/or shoot me
down.

Personally, I believe that some of this is just aesthetics, and some of
it is actually aiming to make you think about making your libraries’
intent as clear as possible.
I think you can refactor as you go, and when the specifications read
close to natural language, you’ve gotten somewhere.

Re: Your specific question about what RSpec brings that C# can’t
achieve…
I think it’s the same as most ruby vs. C# issues, ruby gets out of our
way, where C# simply isn’t able to.

I’d say the closest you get (arguably) in C# is MSpec:

http://codebetter.com/blogs/aaron.jensen/archive/2008/05/08/introducing-machine-specifications-or-mspec-for-short.aspx
http://github.com/machine/machine.specifications/tree/master

Where you have to work around what the C# compiler needs, you have to
write things like:

Because of = () =>
fromAccount.Transfer(1m, toAccount);

Check here for a simple MSpec example:
http://github.com/machine/machine.specifications/blob/0db4d72c93fdc4f5775d9b1eb6239b64a76ad562/Source/Machine.Specifications.Example/BankingSpecs.cs

I’m not trying to knock MSpec at all, but if you look at what you’ve
written above to achieve the same context/specification style, I think
you might agree ruby does a better job getting out of your way to let
you do what you intend.

The C# compiler needs a lot more to be happy, so MSpec does its very
best to get around that.

There is a lot of good material around RSpec and Cucumber, and the art
of writing specifications for it as well.

Thanks Kevin for the detailed answer. I agree that Spec makes easier to
perform BDD style testing. Although I have never used MSpec for C#. I
usually write my C# BDD style test manually without any tools. Like the
following:

public class when_1_is_passed_to_the_prime_service
{

public void should_make_sure_true_is_returned()
{

}

}

Thanks for the input!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs