Testing relationships in ActiveRecord

2 views
Skip to first unread message

Joe

unread,
Jan 22, 2010, 5:17:12 PM1/22/10
to pickle
Hi,

I'm totally new to Ruby and am trying to get a little more familiarity
with Rails / ActiveRecord. In trying to do so I am attempting to use
Cucumber to help with some "discovery" tests.

I have the following

Feature: Describing a task
In order to work with tasks
As a user
I want to be able to know what makes up a task and to improve my
understanding of ActiveRecord


Scenario: A task has certain core fields
Given the following tasks exist
| id | name |
| 1 | some task |
And the following estimates exist
| task_id | hours | explanation |
| 1 | 8 | initial estimate |
| 1 | 6 | better undertsanding of task |
| 1 | 16 | no control over inputs to create task |
| 2 | 22 | for other task |

Then a task: "task" should exist with name: "some task" #this
works
Then the estimate "estimate" should exist with explanation:
"initial estimate" #this works
Then the estimate "estimate" should be one of task: "task"'s
estimates #this works
Then the task "task" should have 3 estimates #this one fails

I have no custom steps - trying to use use what comes out of the box
with cucumber and pickle (just to limit my confusion).

The models are

class Estimate < ActiveRecord::Base

belongs_to :Task

end

and

class Task < ActiveRecord::Base
has_many :estimates
end

Can anyone point me in the right direction (or if I need to post more
code)?

Thanks,

Joe

Michael MacDonald

unread,
Jan 23, 2010, 9:06:35 AM1/23/10
to pickle-...@googlegroups.com
Hi Joe,

Welcome to Ruby, Rails, Cucumber and Pickle!

Here’s my refactor of your example code:

Scenario: A task has certain core fields

Given the following tasks exist:
| name |
| some task |
| another task |
And the following estimates exist:
| task | hours | explanation |
| the 1st task | 8 | initial estimate |
| the 1st task | 6 | better undertsanding of task |
| the 1st task | 16 | no control over inputs to create task |
| the 2nd task | 22 | for other task |
Then a task should exist with name: "some task"
And an estimate should exist with explanation: "initial estimate"
And the estimate should be one of the task's estimates
And the task should have 3 estimates

All will pass except for the last. The default Pickle steps don’t cover checking the size of an association or a collection (although to me that sounds like something good to have). You can still use Pickle to help in writing a custom step definition though:

# only works with plurals eg 3 estimates, not 1 estimate
Then /^#{capture_model} should have (\d+) (\w+)$/ do |name, count, collection|
model(name).should have(count.to_i).send(collection)
end

Notice that I’ve avoided setting task_id. I’ve used the association instead and pickle’s knowledge of the tasks you created earlier.

If there’s anything else I’ve done above you’d like me to explain, just ask.

Cheers,

Michael.

> --
> You received this message because you are subscribed to the Google Groups "pickle" group.
> To post to this group, send email to pickle-...@googlegroups.com.
> To unsubscribe from this group, send email to pickle-cucumb...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pickle-cucumber?hl=en.
>

Joe

unread,
Jan 23, 2010, 7:29:18 PM1/23/10
to pickle
Excellent. Thank you very much for that --works great.

On Jan 23, 9:06 am, Michael MacDonald <mich...@starclass.com.au>
wrote:

Reply all
Reply to author
Forward
0 new messages