rspec 重构? [英] rspec refactoring?

查看:50
本文介绍了rspec 重构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了以下测试,其中包含两个几乎相同的块.现在我正在寻找干净地重构它的方法.

I have the following test, with two almost identical blocks. Now i am looking for ways to refactor this cleanly.

测试:

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

As you can tell, two identical code blocks, each for a different distributor, D1 and D2 (in our project they have more meaningful names :)) -- and now i need to add a third distributor. How do i go about this?

我可以遍历一个包含变化部分的数组(在这种情况下:distributor-name 和消息内容).但是我也可以更改测试名称吗?

I can loop over an array containing the changing parts (in this case: distributor-name and the message contents). But can i also change the test-name?

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

What are the best approaches here? Is it possible to make some kind of test-template, where you can fill in certain values and execute that?

推荐答案

是的,您可以遍历一个充满示例的数组/散列,是的,您可以基于此重命名上下文,但您必须注意范围问题- 例如,上下文是类级别的范围,而测试是一个实例.因此,您必须在上下文的设置"部分的实例变量中设置这些内容.我主要用 unit:test+should(而不是 rspec)来完成这些东西,所以我可能有点搞砸了范围规则,但它们应该是相似的

Yes, you can loop through an array/hash full of examples and yes you can rename contexts based on that but you'll have to be aware of scoping issues - eg a context is a class-level scope, but a test is an instance. Thus you have to setup these things in instance-variables in the "setup" section of a context. I've mainly done this stuff with unit:test+shoulda (rather than rspec) so I may have messed up the scoping rules somewhat, but they should be similarish

注意:我还没有测试过下面的代码,所以它可能是此类问题的牺牲品...

Note: I haven't tested the code below, so it may be prey to such issues...

# name this better than I have    
CONFIGS = {'d1' => {:name => Distributor::D1
                    :destination => '32495xxxxxx',
                    :message => 'd1 message'}, 
           'd2' => {:name => Distributor::D2
                    :destination => '98765xxxxxx',
                    :message => 'd2 message'}
           } # etc

CONFIGS.each do |display_name, dist_hash|
  context "with the #{display_name} configuration" do
    before (:each) do
      # scope the value-hash here to make it available to test-cases 
      # (you don't have to if you're just using it in the setup section)
      @dist_hash = dist_hash
      # send a message 
      @envelope = Factory(:envelope, :destination => @dist_hash[:destination], :message => @dist_hash[:message])
      @distributor = Distributor.find_by_name(@dist_hash[:name])
      @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
end

这篇关于rspec 重构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆