# Test rails model with Cucumber?

**URL:** <https://www.ruby-forum.com/t/test-rails-model-with-cucumber/177217>\
**Category:** Rails\
**Created:** [October 13, 2009, 3:04pm UTC](https://www.ruby-forum.com/t/test-rails-model-with-cucumber/177217 "2009-10-13T15:04:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![builtbysimple](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/aca169/32.png) [@builtbysimple](https://www.ruby-forum.com/u/builtbysimple)\
**Post date:** [October 13, 2009, 3:04pm UTC](https://www.ruby-forum.com/t/test-rails-model-with-cucumber/177217/1 "2009-10-13T15:04:14Z")

</div>

Hi all,

Can I test my rails model with cucumber? Or is this not what cucumber  
is used for? If it is, can anyone lead me to documentation on how to  
use cuke to test Rails model please. I have read the cuke wiki rails  
section, but I still can not understand how to use cuke to test my  
model.

Many thanks for your help.

–  
Certified Scrum Master  
[http://blog.scrum8.com](http://blog.scrum8.com) | [http://jobs.scrum8.com](http://jobs.scrum8.com) |  
[http://twitter.com/scrum8](http://twitter.com/scrum8)

---

<div class="post-metadata">

**Author:** ![builtbysimple](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/aca169/32.png) [@builtbysimple](https://www.ruby-forum.com/u/builtbysimple)\
**Post date:** [October 13, 2009, 3:55pm UTC](https://www.ruby-forum.com/t/test-rails-model-with-cucumber/177217/2 "2009-10-13T15:55:06Z")

</div>

Joshua P. wrote:

> Hi all,
> 
> Can I test my rails model with cucumber? Or is this not what cucumber  
> is used for? If it is, can anyone lead me to documentation on how to  
> use cuke to test Rails model please. I have read the cuke wiki rails  
> section, but I still can not understand how to use cuke to test my  
> model.

I don’t see how this is a good use of Cucumber. Cucumber seems to be  
best for testing user interface.

> Many thanks for your help.
> 
> –  
> Certified Scrum Master  
> [http://blog.scrum8.com](http://blog.scrum8.com) | [http://jobs.scrum8.com](http://jobs.scrum8.com) |  
> [http://twitter.com/scrum8](http://twitter.com/scrum8)

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![builtbysimple](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/aca169/32.png) [@builtbysimple](https://www.ruby-forum.com/u/builtbysimple)\
**Post date:** [October 13, 2009, 3:55pm UTC](https://www.ruby-forum.com/t/test-rails-model-with-cucumber/177217/3 "2009-10-13T15:55:44Z")

</div>

Cucumber is meant to be used for integration testing. You should use  
one of the unit test frameworks (rspec, shoulds, test::unit) to test  
your models specifically.

–Tim R.

---

<div class="post-metadata">

**Author:** ![builtbysimple](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/aca169/32.png) [@builtbysimple](https://www.ruby-forum.com/u/builtbysimple)\
**Post date:** [October 15, 2009, 7:53pm UTC](https://www.ruby-forum.com/t/test-rails-model-with-cucumber/177217/4 "2009-10-15T19:53:41Z")

</div>

Joshua P. wrote:

> Hi all,
> 
> Can I test my rails model with cucumber? Or is this not what cucumber  
> is used for? If it is, can anyone lead me to documentation on how to  
> use cuke to test Rails model please. I have read the cuke wiki rails  
> section, but I still can not understand how to use cuke to test my  
> model.
> 
> Many thanks for your help.
> 
> –  
> Certified Scrum Master  
> [http://blog.scrum8.com](http://blog.scrum8.com) | [http://jobs.scrum8.com](http://jobs.scrum8.com) |  
> [http://twitter.com/scrum8](http://twitter.com/scrum8)

Actually, I agree with Tim R., cucumber is meant as a Business  
Readable Behavior Description language, but I don’t like non-answer  
answers.

It absolutely CAN be used to test models, although it is not the best  
option, currently IMHO the best choice at least for Rails models, is  
Rspec+Shoulda alone, but here’s an exmaple of one way to do it (on top  
of rspec and shoulda):

[http://www.pastie.org/656364](http://www.pastie.org/656364)

I hope it answers your question, although is extremely verbose for a  
model unit test, where a simple rspec test would suffice and be much  
more expressive for a programmer, after all unit tests are not meant to  
be seen or used by customers, in that area, Cucumber is the way to go  
right now.

---

<div class="post-metadata">

**Author:** ![builtbysimple](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/aca169/32.png) [@builtbysimple](https://www.ruby-forum.com/u/builtbysimple)\
**Post date:** [October 14, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/test-rails-model-with-cucumber/177217/5 "2009-10-14T18:45:46Z")

</div>

IMHO Cucumber is an excellent tool to use to on top of rspec to do unit  
testing.

Dear Joshua,

Doing unit tests with cucumber has not yet been approved as a best  
practice,  
but don’t let that stop you from experimenting.

Cucumber is no substitute for rspec , it’s a complement, a  
way of writing tests in a more natural language, using rspec’s power  
underneath.  
Thus, it is not only suited for integration tests, but for view test,  
database tests,  
and anything else that you can think of.

Jim W. has started an implementation of unit test with cucumber.

> **[GitHub - jimweirich/Given: A Given/When/Then Specification Framework](https://github.com/jimweirich/Given)**
>
> A Given/When/Then Specification Framework. Contribute to jimweirich/Given development by creating an account on GitHub.

I recommend you simply write in cucumber what you need to test,  
and figure out how to make it reusable to other models.

A good friend has started an implementation of reusable steps  
in Spanish, soon it will be available in English too.

> **[GitHub - nando/mundo-pepino: Set of reusable step definitions to test a Rails...](https://github.com/nando/mundo-pepino)**
>
> Set of reusable step definitions to test a Rails application with Cucumber - GitHub - nando/mundo-pepino: Set of reusable step definitions to test a Rails application with Cucumber

All the best!

Raimond G.
