0

我有一个关系:Transaction belongs_to Post(has_many)

我有这样的看法:

# /app/views/transactions/index.html.haml

%h3 Transactions
%table
  %tr 
    %th Item
    %th Description
    %th Money
  - @transactions.each do |transaction|
    %tr 
      %td= transaction.description
      %td= number_to_currency(transaction.money)
      %td= link_to transaction.post.title, transaction.post.link # (!)

如果我没有最后一行 - 我会像这样测试它:

before(:each) do
  assign(:transactions, [
    stub_model(Transaction,
      { description: "Posted", money: 9.99 }
    ),
    stub_model(Transaction,
      { description: "Posted", money: 9.99 }
    )
  ])
end

it "renders a list of transactions" do
  render
  assert_select "tr>th", :text => "Description"
  assert_select "tr>th", :text => "Money"
  assert_select "tr>td", :text => "Posted", :count => 2
  assert_select "tr>td", :text => number_to_currency(9.99).to_s, :count => 2
end

但是我不知道如何测试视图的最后一行,它显示了它所属的帖子的信息。

我已经阅读过stub_chain,但无法弄清楚如何将其安装到存根 entier 模型中。
尝试先存根 Post,然后为存根事务分配 post_id - 也没有工作..

还是我应该在 Capybara 级别上对其进行测试?

4

1 回答 1

0

找到了办法。

刚刚做了:

before(:each) do
  assign(:transactions, [
    stub_model(Transaction,
      { description: "Posted", money: 9.99, post: mock_model(Post, FactoryGirl.attributes_for(:post)) }
    ),
    stub_model(Transaction,
      { description: "Posted", money: 9.99, post: mock_model(Post, FactoryGirl.attributes_for(:post)) }
    )
  ])
end
于 2013-10-18T15:34:58.567 回答