Is it possible to use email-spec with Shoulda?

80 views
Skip to first unread message

Andrew Stewart

unread,
Jun 4, 2010, 6:44:28 AM6/4/10
to shoulda
Hello,

I happily test my Rails app with Shoulda and Cucumber. I've just
started using email-spec [1] to test the emails my app sends, but
email-spec is not Test::Unit compatible; it's RSpec all the way.

I'm vaguely aware that Shoulda has some kind of RSpec-compatible
syntax though I've never paid attention to it before because I've
never needed to, and I don't really understand how it works.

Is it possible to mix Shoulda into email-spec so email-spec starts
working? Or do I need to replace each and every RSpec assertion in
email-spec with a Test::Unit equivalent?

[1]: http://github.com/bmabey/email-spec

Thanks and regards,

Andy Stewart
------
http://airbladesoftware.com

Dan Croak

unread,
Jun 4, 2010, 10:01:18 AM6/4/10
to sho...@googlegroups.com
You can use Email Spec with Shoulda and RSpec.

When you use Shoulda with RSpec, what you're doing most of the time is
using Shoulda *matchers* inside RSpec. For example, you could use
Shoulda's ActiveRecord matchers:

describe Report do
it { should belong_to(:project) }
it { should validate_presence_of(:project_id) }
it { should have_db_index(:project_id) }
end

The "have_db_index()"-like methods are the *matchers*.

Email Spec is another library that includes matchers:

Spec::Runner.configure do |config|
config.include(EmailSpec::Helpers)
config.include(EmailSpec::Matchers)
end

It is used like this:

it "should be delivered to the email passed in" do
should deliver_to("jo...@yahoo.com")
end

it "should contain the user's name in the mail body" do
@email.should have_body_text(/Jojo Binks/)
end

> --
> Individuals over processes. Interactions over tools.
>
> Agile Rails training from thoughtbot, the makers of Clearance, Shoulda, & Factory Girl:
> http://thoughtbot.com/services/training
>
> The Shoulda group:
> http://groups.google.com/group/shoulda
>
> To post to this group, send email to
> sho...@googlegroups.com
>
> To unsubscribe from this group, send email to
> shoulda+u...@googlegroups.com
>

Andrew Stewart

unread,
Jun 4, 2010, 10:14:17 AM6/4/10
to sho...@googlegroups.com
On 4 Jun 2010, at 15:01, Dan Croak wrote:
> You can use Email Spec with Shoulda and RSpec.
>
> When you use Shoulda with RSpec, what you're doing most of the time is
> using Shoulda *matchers* inside RSpec. For example, you could use
> Shoulda's ActiveRecord matchers:

Thanks for the explanation. It makes more sense now.

So I assume that I have to use RSpec if I want to use email-spec? I'd like to use email-spec with Test::Unit instead, and I had been wondering whether Shoulda could somehow make email-spec believe it was calling RSpec when in fact it was calling Test::Unit. But since the Shoulda matchers are where Shoulda integrates with RSpec I guess it's not possible. Is that right?

Thanks again,

Andy Stewart
-------
http://airbladesoftware.com

Dan Croak

unread,
Jun 4, 2010, 10:18:37 AM6/4/10
to sho...@googlegroups.com
I don't think you can use matchers in Test::Unit alone. There may be
some extension but at that point, my advice would be to just move to
RSpec.

A nice thing about the transition is that the RSpec test runner can
run Test::Unit/Shoulda suites, so if you have an existing TU suite,
can just convert tests to RSpec as you change them and write in RSpec
for new code while the whole suite continues to run.

Andrew Stewart

unread,
Jun 4, 2010, 10:21:27 AM6/4/10
to sho...@googlegroups.com
On 4 Jun 2010, at 15:18, Dan Croak wrote:
> I don't think you can use matchers in Test::Unit alone. There may be
> some extension but at that point, my advice would be to just move to
> RSpec.
>
> A nice thing about the transition is that the RSpec test runner can
> run Test::Unit/Shoulda suites, so if you have an existing TU suite,
> can just convert tests to RSpec as you change them and write in RSpec
> for new code while the whole suite continues to run.

That's good to know. Thanks for all the help!

Brian Cardarella

unread,
Jun 4, 2010, 9:41:36 PM6/4/10
to shoulda
Andrew,

There is actually no 'RSpec' in email-spec. RSpec is based upon the
idea of matchers. Which is a single method on a class called
'matches?' that will return true or false. This method is called in
RSpec to run the test.

That being said you do not have to modify the email-spec code to
have it run in TestUnit. However, you will have to write helpers to
wrap the email-spec matchers. For a guide on how to do this you can
actually look at the Shoulda code itself. Check out the Shoulda
ActiveRecord macros files:

http://github.com/thoughtbot/shoulda/blob/master/lib/shoulda/active_record/macros.rb

For example the TestUnit macro 'should_validates_presence_of' just
calls the matcher code.

- Brian

Andrew Stewart

unread,
Jun 7, 2010, 6:11:32 AM6/7/10
to sho...@googlegroups.com
On 5 Jun 2010, at 02:41, Brian Cardarella wrote:
> There is actually no 'RSpec' in email-spec. RSpec is based upon the
> idea of matchers. Which is a single method on a class called
> 'matches?' that will return true or false. This method is called in
> RSpec to run the test.
>
> That being said you do not have to modify the email-spec code to
> have it run in TestUnit. However, you will have to write helpers to
> wrap the email-spec matchers. For a guide on how to do this you can
> actually look at the Shoulda code itself. Check out the Shoulda
> ActiveRecord macros files:
>
> http://github.com/thoughtbot/shoulda/blob/master/lib/shoulda/active_record/macros.rb
>
> For example the TestUnit macro 'should_validates_presence_of' just
> calls the matcher code.

This is very much what I was hoping would be the case -- thanks for clarifying it.

I had a go at writing a single wrapper ("macro") for email-spec, with the aim of calling that from a step definition instead of the out-of-the-box matcher:

http://gist.github.com/428491

Unfortunately I ran into a `uninitialized constant Shoulda::Assertions (NameError)`. Is there a simple solution to this?

Reply all
Reply to author
Forward
0 new messages