0

这个问题是关于我做的一个测试,以验证用户页面中的所有 50 个微博都显示

我有这个 rspec 代码:

          before(:all) { 50.times {FactoryGirl.create(:micropost, 
                                   user:user, content: "Lorem Ipsum") } }

我想让每个内容更加动态,例如:

"Lorem Ispum 0"
"Lorem Ipsum 1"
"Lorem Ipsum 2"
...

我试着写:

let(:count_helper) { 0 }
before(:all) { 50.times {FactoryGirl.create(:micropost, 
                                       user:user, 
                                       content: "Lorem Ipsum #{count_helper}") 
                         count_helper += 1} }

它在这里失败了:

count_helper += 1 

怎样才能写对?

4

2 回答 2

2
let(:count_helper) { 0 }
before(:all) { 50.times {FactoryGirl.create(:micropost, 
                                       user:user, 
                                       content: "Lorem Ipsum #{count_helper}");
                                       count_helper += 1 } }

注意末尾的分号FactoryGirl.create(:micropost, user:user, content: "Lorem Ipsum #{count_helper}")

由于您使用的是单行块语法,因此您应该明确告诉 Ruby 这count_helper += 1是另一个语句。

于 2012-08-25T12:28:38.417 回答
0

这就是代码的编写方式:

    before(:all) { 50.times do |count|
                     FactoryGirl.create(:micropost, user:user, 
                     content: "Lorem Ipsum #{count}") 
                   end }

我有两个错误:

  1. 当内部只允许一个时创建 2 个代码行{}- 我将其更改为do ... end

  2. 我添加了 |count| 循环的变量,帮助我计算循环

于 2012-08-25T12:40:32.023 回答