Using Shoulda with Test/Unit in Rails 3

Curious if anyone has Shoulda working with test/unit in Rails 3. I tried it this morning and am getting:

test/unit/practice_member_test.rb:4:in <class:PracticeMemberTest>': undefined method context’ for PracticeMemberTest:Class (NoMethodError)

Now, I have a Rails 3 project with Rspec including Shoulda also and it works fine. I tried placing “require ‘shoulda’” in test helper to no avail, but when I run the debugger and type Shoulda, the object is found.

It it helps, here is my test:

require ‘test_helper’

class PracticeMemberTest < Test::Unit::TestCase context “practice member” do
should “get global practice member count not including Demo Practice” do assert_equal PracticeMember.practice_members_global_count, 0

  practice = Factory.create(:practice_one)

  practice_member = Factory.create(:practice_member)
  practice_member.practice_id = [practice.id](http://practice.id)
  practice_member.save
  practice_member = Factory.create(:practice_member)
  practice_member.practice_id = [practice.id](http://practice.id)
  practice_member.save

  assert_equal PracticeMember.practice_members_global_count, 2
end

end end

David Kahn wrote in post #968428:

Curious if anyone has Shoulda working with test/unit in Rails 3. I tried it this morning and am getting:

test/unit/practice_member_test.rb:4:in `<class:PracticeMemberTest>': undefined method `context' for PracticeMemberTest:Class (NoMethodError)

Now, I have a Rails 3 project with Rspec including Shoulda also and it works fine. I tried placing "require 'shoulda'" in test helper to no avail, but when I run the debugger and type Shoulda, the object is found.

What's the point of using RSpec and Shoulda together? I thought all Shoulda did was give an RSpec-like syntax to Test::Unit.

Come to think of it, what's the point of using Shoulda (instead of RSpec) at all?

Best,

David Kahn wrote in post #968428:

Curious if anyone has Shoulda working with test/unit in Rails 3. I tried

it

this morning and am getting:

test/unit/practice_member_test.rb:4:in `class:PracticeMemberTest’:

undefined method `context’ for PracticeMemberTest:Class (NoMethodError)

Now, I have a Rails 3 project with Rspec including Shoulda also and it

works

fine. I tried placing “require ‘shoulda’” in test helper to no avail,

but

when I run the debugger and type Shoulda, the object is found.

What’s the point of using RSpec and Shoulda together? I thought all

Shoulda did was give an RSpec-like syntax to Test::Unit.

Come to think of it, what’s the point of using Shoulda (instead of

RSpec) at all?

Well, I like that I can include it in Test/Unit. Not really into the .should and the argument that it makes tests readable… just does not feel necessary to me. I like the “should …” vs “argument.should be_nil” or the like. Just reads better to me.

One less DSL to use (arguably)

One of the things I really like in Shoulda that I have seen so far are the matchers… things like:

it { should belong_to(:contact) } it { should have_many(:addresses) } it { should validate_presence_of(:address) }

it { should allow_value(‘55416’).for(:zip) } it { should_not allow_value(‘554316’).for(:zip) }

I think these are very cool… and save a lot of time. This is what made me look into shoulda. Does Rspec have these/similar?

I think the documentation is better for Shoulda, not that this means everything but is important. And Shoulda gives me what I consider the best feature of Rspec - the grouping syntax (describe… it …)… that is the one thing to date that I felt I would miss of empirical value by not using Rspec.

So why not? What am I missing?

David Kahn wrote in post #968465:

> fine. I tried placing "require 'shoulda'" in test helper to no avail, > but > when I run the debugger and type Shoulda, the object is found.

What's the point of using RSpec and Shoulda together? I thought all Shoulda did was give an RSpec-like syntax to Test::Unit.

Come to think of it, what's the point of using Shoulda (instead of RSpec) at all?

Well, I like that I can include it in Test/Unit. Not really into the .should and the argument that it makes tests readable...

So you're saying you don't think that value.should == 3 is more readable than assert_equal 3, value ?

First of all, I can't imagine how the English-like syntax *isn't* the more readable; second, if that's so, then why are you using Shoulda at all.

just does not feel necessary to me.

I assure you, it is necessary. The assert_ syntax of Test::Unit lends itself to writing unreadable, implementation-focused tests. RSpec-style "should" syntax encourages you to think about behavior, as well as keeping the code more readable.

I really don't know why anyone would voluntarily choose Test::Unit, unless it be inertia.

I like the "should ....." vs "argument.should be_nil" or the like. Just reads better to me.

I just looked up Shoulda's documentation to see the differences. I don't see how you can seriously claim that Shoulda reads better than RSpec. The use of the word "should" simply appears to be poorly copied from RSpec without serious thought about the semantics -- as evidenced by the fact that "should" can't begin an assertion in English or RSpec, but can in Shoulda. Yuck!

One less DSL to use (arguably)

Argument lost. It's still a DSL, just a poorly designed one. So use a better designed one. That means RSpec.

One of the things I really like in Shoulda that I have seen so far are the matchers... things like:

  it { should belong_to(:contact) }   it { should have_many(:addresses) }   it { should validate_presence_of(:address) }   it { should allow_value('55416').for(:zip) }   it { should_not allow_value('554316').for(:zip) }

I think these are very cool... and save a lot of time. This is what made me look into shoulda.

Then IMHO you are using Shoulda for entirely the wrong reasons.

Does Rspec have these/similar?

RSpec has many similar methods. Not the ones you quoted above, exactly, but those would be easy to write.

I think the documentation is better for Shoulda, not that this means everything but is important.

Haven't looked at the Shoulda docs in detail, so can't comment.

And Shoulda gives me what I consider the best feature of Rspec - the grouping syntax (describe... it ....).... that is the one thing to date that I felt I would miss of empirical value by not using Rspec.

So why not? What am I missing?

Almost everything, apparently. :slight_smile: Shoulda seems to me to be a cheap knockoff of RSpec with less readable and less well thought out syntax, and improper semantics. Why use the cheap knockoff when you could use the real thing?

Have you even tried RSpec?

Best,

the whole context syntax is been deprecated in favor of a pure rspec like syntax.

http://robots.thoughtbot.com/post/701863189/shoulda-rails3-and-beyond

from thoughbot

'We’re committed to using RSpec, so we don’t want to spend time adding features to Shoulda’s context framework. ’

from thoughbot

'We’re committed to using RSpec, so we don’t want to spend time adding features to Shoulda’s context framework. ’

Thanks… I found and read the article the quote came from - http://robots.thoughtbot.com/post/701863189/shoulda-rails3-and-beyond - which pretty much puts the nail in the coffin :slight_smile:

David Kahn wrote in post #968465:

fine. I tried placing “require ‘shoulda’” in test helper to no avail,

but

when I run the debugger and type Shoulda, the object is found.

What’s the point of using RSpec and Shoulda together? I thought all

Shoulda did was give an RSpec-like syntax to Test::Unit.

Come to think of it, what’s the point of using Shoulda (instead of

RSpec) at all?

Well,

I like that I can include it in Test/Unit.

Not really into the .should and the argument that it makes tests

readable…

So you’re saying you don’t think that

value.should == 3

is more readable than

assert_equal 3, value

?

First of all, I can’t imagine how the English-like syntax isn’t the

more readable; second, if that’s so, then why are you using Shoulda at

all.

just does not feel necessary to me.

I assure you, it is necessary. The assert_ syntax of Test::Unit lends

itself to writing unreadable, implementation-focused tests. RSpec-style

“should” syntax encourages you to think about behavior, as well as

keeping the code more readable.

I really don’t know why anyone would voluntarily choose Test::Unit,

unless it be inertia.

I like the “should …” vs “argument.should be_nil” or the like. Just

reads better to me.

I just looked up Shoulda’s documentation to see the differences. I

don’t see how you can seriously claim that Shoulda reads better than

RSpec. The use of the word “should” simply appears to be poorly copied

from RSpec without serious thought about the semantics – as evidenced

by the fact that “should” can’t begin an assertion in English or RSpec,

but can in Shoulda. Yuck!

One less DSL to use (arguably)

Argument lost. It’s still a DSL, just a poorly designed one. So use a

better designed one. That means RSpec.

One of the things I really like in Shoulda that I have seen so far are

the

matchers… things like:

it { should belong_to(:contact) }

it { should have_many(:addresses) }

it { should validate_presence_of(:address) }

it { should allow_value(‘55416’).for(:zip) }

it { should_not allow_value(‘554316’).for(:zip) }

I think these are very cool… and save a lot of time. This is what made

me

look into shoulda.

Then IMHO you are using Shoulda for entirely the wrong reasons.

Does Rspec have these/similar?

RSpec has many similar methods. Not the ones you quoted above, exactly,

but those would be easy to write.

And where would I learn about them? Perhaps I missed it but even in the Rspec book I dont see any mention of such coolness. I know I can use shoulda with rspec and get its helpers but if you know some specifics that Rspec does this as concisely — like validation and associations if you can point me in the right direction I would appreciate it… that way I will forget about shoulda.

David Kahn wrote in post #968911:

>> value.should == 3 I assure you, it is necessary. The assert_ syntax of Test::Unit lends I just looked up Shoulda's documentation to see the differences. I better designed one. That means RSpec. >

And where would I learn about them? Perhaps I missed it but even in the Rspec book I dont see any mention of such coolness.

Learn about what coolness? It's not clear from your quotation.

I know I can use shoulda with rspec and get its helpers but if you know some specifics that Rspec does this as concisely --- like validation and associations if you can point me in the right direction I would appreciate it...

RSpec doesn't have the canned validation and association matchers that Shoulda does, but they're pretty easy to write for yourself if you want to. Some of my stuff on Github should give you ideas...reflect_on_association is *very* useful.

that way I will forget about shoulda.

Best,