gpt4 book ai didi

ruby-on-rails - rspec 重构?

转载 作者:行者123 更新时间:2023-12-04 06:25:31 24 4
gpt4 key购买 nike

我有以下测试,有两个几乎相同的 block 。现在我正在寻找彻底重构它的方法。

测试:

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(在我们的项目中,它们具有更有意义的名称:))——现在我需要添加第三个分销商。我该怎么做?

我可以遍历一个包含变化部分的数组(在本例中:distributor-name 和消息内容)。但是我也可以更改测试名称吗?

这里最好的方法是什么?是否可以制作某种测试模板,您可以在其中填写某些值并执行?

最佳答案

我与我的一位更有经验的同事进行了一次结对编程 session ,我们一起提出了以下解决方案。

我们首先定义了一些共享行为:

subject {@envelope}
let(:the_sent_message){ @envelope.sent_messages.find_by_distributor_id(@distributor.id)}

shared_examples_for "a typical sent envelope" do
it{should have(1).sent_messages }
it{should have(2).log_lines }
end

shared_examples_for "a successful delivery" do
it("should have 1 IN_PROGRESS sms-message") { the_sent_message.should be_in_progress }

it "should have 1 sms-message with external ref" do
the_sent_message.external_message_id.should_not == nil
end

it "should log the delivery success" do
@envelope.log_lines.last.message.should =~ /^Sent message/
end
end

shared_examples_for "a failing delivery" do
it("should have 1 FAILED sms-message") { the_sent_message.should be_failed }

it "should have 1 sms-message and no external ref" do
the_sent_message.external_message_id.should == nil
end

it "should log the delivery failure" do
@envelope.log_lines.last.message.should =~ /^Failed to send/
end
end

然后测试变得更具可读性!

context "delivered by d1" do
before do
@distributor = Distributor.find_by_name(Distributor::D1)

send_a_test_envelope_to(@distributor)
end

it_should_behave_like "a typical sent envelope"
it_should_behave_like "a successful delivery"
end

context "delivered by d2" do
before do
@distributor = Distributor.find_by_name(Distributor::D2)

send_a_test_envelope_to(@distributor)
end

it_should_behave_like "a typical sent envelope"
it_should_behave_like "a successful delivery"
end

而且我们还提取了下面的方法

def send_a_test_envelope_to(distributor)
@envelope = Factory(:envelope, :destination => '32495xxxxxx', :message => "Message sent by #{@distributor.name}")
@envelope.send_to(distributor)
end

现在我仍然可以应用@Taryn 提出的建议答案,但我不完全确定是否真的需要它。

关于ruby-on-rails - rspec 重构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3307416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com