【发布时间】:2015-12-12 11:08:32
【问题描述】:
所以我需要一个辅助函数来创建“未处理的推文”,类似于我从 Twitter API gem 接收它们的方式,这样我就可以在特定条件下测试我的模型功能。
为此,我在对象描述中添加了一个辅助函数,如下所示:
describe Tag, :type => :model do
# Helpers
###
def unprocessed_tweets(count, name, start_date, end_date)
tweets = []
count.times do |index|
tweet = OpenStruct.new
tweet.favorite_count = "3"
tweet.filter_level = "high"
tweet.retweet_count = "12"
tweet.text = "#{name}"
if index == 0
tweet.created_at = start_date
elsif index == (count-1)
tweet.created_at = end_date
else
tweet.created_at = start_date
end
tweets.push tweet
end
tweets
end
我还添加了一个测试,以确保我的助手长期按照我的预期行事:
it "has a helper for generated unprocessed tweets" do
tag_name = "justin"
start_date = '2015-09-12 2:31:32 0'
end_date = '2015-09-13 2:31:32 0'
tweets = unprocessed_tweets(3, tag_name, start_date, end_date)
expect(tweets.size).to eq 3
expect(tweets.first.favorite_count).to eq "3"
expect(tweets.first.created_at).to eq start_date
expect(tweets.last.created_at).to eq end_date
expect(tweets.last.text).to eq tag_name
end
这是最好的做法吗?
【问题讨论】:
标签: ruby-on-rails rspec stubbing