【发布时间】:2014-09-05 14:13:00
【问题描述】:
在编码方面,我最弱的一点是使用 TDD 和 BDD 方法 - 我倾向于只编写代码.. 但这是我正在努力的事情。
谁能指出解决以下问题的最佳方法:
第一类:
module TempMod
class MyClass
def initalize(config)
@config = config
end
def process(xml)
if react_upon? xml.something
puts 'yeah'
else
puts 'nah'
end
end
def react_upon?(xml_code)
#code here
end
end
end
假设我想测试这个类,或者从 TDD 的角度构建它,所以我编写了我的测试:
describe TempMod::MyClass do
let(:config) {double}
let(:myclass) {TempMod::MyClass.new config}
context 'Given that the xml is something we react upon' do
it 'should check that it is valid' do
myclass.process '<some><xml>here</xml></some>'
end
it 'should output yea'
end
end
如何测试它是否调用了 react_upon?方法。我什至想看看它在调用它吗?
测试它的正确方法是测试 react_upon 之类的所有功能吗?自身独立于其他功能?
这正是这种测试最让我困惑的主要事情。我是在测试整个班级,还是只是单独测试函数,而不是测试它们与该班级中其他函数的交互?
我也意识到了react_upon?可能不遵守单一职责原则,我可能会将其移到它自己的模块/类中,我可以使用存根进行测试。
如果有人能为我阐明这一点,那就太棒了。
编辑:
describe TempMod::MyClass do
let (:valid_planning_status_xml) {
'<StatusUpdate> <TitleId>2329</TitleId> <FromStatus>Proposed</FromStatus> <ToStatus>Confirmed</ToStatus> </StatusUpdate>'
}
let(:config) { double }
let(:status_resolver) { double }
subject(:message_processor) { TempMod::MyClass.new config, status_resolver }
context 'Given that the message XML is valid' do
it 'should check the context of the message' do
expect(message_processor.process valid_planning_status_xml).to call :check_me
end
context 'Given that the message is for a planning event update' do
it 'should call something' do
pending
end
end
context 'Given that the message is for a recording job update' do
end
context 'Given that the message is for a video title update' do
end
end
end
【问题讨论】: