# RSpec Test Format

**URL:** <https://www.ruby-forum.com/t/rspec-test-format/171645>\
**Category:** IronRuby\
**Created:** [July 7, 2009, 4:11am UTC](https://www.ruby-forum.com/t/rspec-test-format/171645 "2009-07-07T04:11:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![metanoize](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@metanoize](https://www.ruby-forum.com/u/metanoize)\
**Post date:** [July 7, 2009, 4:11am UTC](https://www.ruby-forum.com/t/rspec-test-format/171645/1 "2009-07-07T04:11:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![metanoize](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@metanoize](https://www.ruby-forum.com/u/metanoize)\
**Post date:** [July 7, 2009, 8:44pm UTC](https://www.ruby-forum.com/t/rspec-test-format/171645/2 "2009-07-07T20:44:27Z")

</div>

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://codebetter.com/blogs/aaron.jensen/archive/2008/05/08/introducing-machine-specifications-or-mspec-for-short.aspx)

> **[GitHub - machine/machine.specifications: Machine.Specifications is a...](https://github.com/machine/machine.specifications/tree/master)**
>
> 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:

> <https://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.

---

<div class="post-metadata">

**Author:** ![metanoize](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@metanoize](https://www.ruby-forum.com/u/metanoize)\
**Post date:** [July 7, 2009, 8:48pm UTC](https://www.ruby-forum.com/t/rspec-test-format/171645/3 "2009-07-07T20:48:14Z")

</div>

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!
