我正在尝试更熟悉Rails / ActiveRecord。在尝试这样做时,我试图使用Cucumber来帮助进行一些“发现”测试。
我有以下
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
修改
我没有自定义步骤 - 尝试使用黄瓜和泡菜盒子里的东西(只是为了限制我的困惑)。
模型
class Estimate < ActiveRecord::Base
belongs_to :Task, :class_name => "Task", :foreign_key => "Task_id"
end
和
class Task < ActiveRecord::Base
has_many :estimates
end
有人能指出我正确的方向(或者我是否需要发布更多代码)?
谢谢,
乔
答案 0 :(得分:0)
您的估算课程可能如下所示:
class Estimate ...
belongs_to :task
end
它将推断表名和fk并假设你一直在关注rails数据库习语,它应该都可以正常工作。
至于你的cuke步骤,我从来没有使用过泡菜,所以我不确定是怎么回事,但是如果失败的步骤是:
Then the task "task" should have 3 estimates #this one fails
这可能与我上面概述的变化有关(也许它正在用表名做一些奇怪的事情?)。