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 methodcontext’ 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
  practice_member.save
  practice_member = Factory.create(:practice_member)
  practice_member.practice_id = practice.id
  practice_member.save

  assert_equal PracticeMember.practice_members_global_count, 2
end

end
end

David K. 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,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On Tue, Dec 14, 2010 at 4:26 PM, Marnen Laibow-Koser
[email protected]wrote:

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?

On Tue, Dec 14, 2010 at 6:17 PM, David K.
[email protected]wrote:

  practice = Factory.create(:practice_one)

end
end

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

David K. wrote in post #968465:

On Tue, Dec 14, 2010 at 4:26 PM, Marnen Laibow-Koser
[email protected]wrote:

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,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Sent from my iPhone

from thoughbot

'Were committed to using RSpec, so we dont want to spend time adding
features to Shouldas context framework. ’

On Wed, Dec 15, 2010 at 8:51 AM, radhames brito [email protected]
wrote:

from thoughbot

'Were committed to using RSpec, so we dont want to spend time adding
features to Shouldas context framework. ’

Thanks… I found and read the article the quote came from -
The future of shoulda -
which pretty much puts the nail in the coffin :slight_smile:

On Wed, Dec 15, 2010 at 8:30 AM, Marnen Laibow-Koser
[email protected]wrote:

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. 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 K. wrote in post #968911:

On Wed, Dec 15, 2010 at 8:30 AM, Marnen Laibow-Koser
[email protected]wrote:

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,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]