【发布时间】:2014-05-18 13:18:03
【问题描述】:
我想测试save方法:
class Note
def initialize(password)
@password = password
end
def save
encryption = Note::Encryption.new(@password)
encrypted = encryption.encrypt(serialized)
storage = Note::Storage.new
storage.write(encrypted)
end
private
def serialized
{some_data: true}
end
# .....
end
方法只是主要将工作委托给其他类,这是唯一的责任。我测试它的第一个赌注只是检查:
describe '#save' do
let(:encryption){ '12345' }
it 'calls encryption' do
expect_any_instance_of(Note::Encryption).to receive(:encrypt)
subject.save
end
it 'saves the file with data' do
expect_any_instance_of(Note::Storage).to receive(:write)
subject.save
end
end
我对这种方法有疑问,因为我有点担心这些测试不会测试太多......此外,现在的测试很高,加上实现而不是行为。有谁知道如何处理这种方法。值得一提的是,这个类将位于系统的顶部,因为它包装了一些资源。
【问题讨论】:
标签: ruby-on-rails ruby rspec tdd sinatra