【问题标题】:Writing Unit tests for a private function.. Unsure of proper strategy为私有函数编写单元测试.. 不确定正确的策略
【发布时间】:2013-11-11 18:07:35
【问题描述】:
我的代码..:
after_save :handle_test
private
def handle_test
if parent.try(:is_test)
Resque.enqueue UnpackTestOnS3, parent.id
end
end
我正在尝试制定测试此模型逻辑的最佳方法。
我是否测试一下 Resque 收到了什么?
我是否测试parent.try(:is_test) 是真的?如果是这样,那将有点愚蠢,因为在测试本身中,我必须对其进行存根或 update_attribute..
这是否太细化而无法测试?
我是否应该只是测试文件是否出现在 S3 上?我不确定将单元测试实际上传到 S3 是否有意义。
热烈欢迎所有策略。
【问题讨论】:
标签:
ruby-on-rails
unit-testing
shoulda
【解决方案1】:
您可以在创建或更新模型时测试是否有一个作业排队等待解包
it "should enqueue job to unpack test on s3 on creating <model_name>" do
Resque.should_receive(:enqueue).with(UnpackTestOnS3, <parent_id>)
end
it "should enqueue job to unpack test on s3 on updating <model_name>" do
Resque.should_receive(:enqueue).with(UnpackTestOnS3, <parent_id>)
end
it "should not enqueue job to unpack test on s3 when the <model_name>'s parent is not for test" do
Resque.should_not_receive(:enqueue).with(UnpackTestOnS3, <parent_id>)
end