【发布时间】:2010-07-22 09:28:49
【问题描述】:
我有以下测试,有两个几乎相同的块。现在我正在寻找干净地重构它的方法。
测试:
context "with the d1 configuration" do
before (:each) do
# send a message
@envelope = Factory(:envelope, :destination => '32495xxxxxx', :message => 'Message sent by d1')
@distributor = Distributor.find_by_name(Distributor::D1)
@result = @envelope.send_to(@distributor)
end
it "should created a new sms-message" do
@envelope.sent_messages.size.should == 1
end
it "should have created one sms-message linked to the envelope and distributor" do
sms = @envelope.sent_messages.find_by_distributor_id(@distributor.id)
sms.should be_instance_of(SentMessage)
sms.external_message_id.should_not == nil
sms.sent_message_status_id.should == SentMessageStatus::IN_PROGRESS
end
it "should add a logline for the creation of the sms-message" do
@envelope.log_lines.size.should == 2
@envelope.log_lines.last.message.should =~ /^Sent message/
end
end
context "with the correct d2 configuration" do
before (:each) do
# send a message
@envelope = Factory(:envelope, :destination => '32495xxxxxx', :message => 'Message sent by d2')
@distributor = Distributor.find_by_name(Distributor::D2)
@result = @envelope.send_to(@distributor)
end
it "should created a new sms-message" do
@envelope.sent_messages.size.should == 1
end
it "should have created one sms-message linked to the envelope and distributor" do
sms = @envelope.sent_messages.find_by_distributor_id(@distributor.id)
sms.should be_instance_of(SentMessage)
sms.external_message_id.should_not == nil
sms.sent_message_status_id.should == SentMessageStatus::IN_PROGRESS
end
it "should add a logline for the creation of the sms-message" do
@envelope.log_lines.size.should == 2
@envelope.log_lines.last.message.should =~ /^Sent message/
end
end
如您所知,两个相同的代码块,每个用于不同的分发器,D1 和 D2(在我们的项目中,它们具有更有意义的名称:))——现在我需要添加第三个分发器。我该怎么办?
我可以遍历一个包含变化部分的数组(在本例中:分发者名称和消息内容)。但是我也可以更改测试名称吗?
这里最好的方法是什么?是否可以制作某种测试模板,您可以在其中填写某些值并执行它?
【问题讨论】:
标签: ruby-on-rails rspec