如何在没有Shoulda的情况下在Rspec中编写一行测试?



我有一堆非常重复的rspec测试,它们都有相同的格式:

it "inserts the correct ATTRIBUTE_NAME" do
     @o.ATTRIBUTE_NAME.should eql(VALUE)
end

如果我能做一行这样的测试就好了:

compare_value(ATTRIBUTE_NAME, VALUE)

但是shoulda似乎并不适合这些类型的测试。还有其他选择吗?

有时我后悔将subject暴露为最终用户设备。它的引入是为了支持扩展(如shoulda匹配器),因此您可以编写如下示例:

it { should do_something }
然而,像这样的例子读起来不太好:
it { subject.attribute.should do_something }

如果您要显式地使用subject,然后在示例中显式地引用它,我建议使用specify而不是it:

specify { subject.attribute.should do_something }

底层语义是相同的,但是这个^^可以被大声读出。

如果你想让它读起来更清晰,而且只有一行,我会写一个自定义的RSpec helper。假设要测试的类如下:

class MyObject
  attr_accessor :first, :last, :phone
  def initialize first = nil, last = nil, phone = nil
    self.first = first
    self.last = last
    self.phone = phone
  end
end

我们可以写下面的匹配器:

RSpec::Matchers.define :have_value do |attribute, expected|
  match do |obj|
    obj.send(attribute) == expected
  end 
  description do
    "have value #{expected} for attribute #{attribute}" 
  end
end

然后我们可以这样写测试:

describe MyObject do
  h = {:first => 'wes', :last => 'bailey', :phone => '111.111.1111'}
  subject { MyObject.new h[:first], h[:last], h[:phone] }
  h.each do |k,v|
    it { should have_value k, v}
  end
end

如果你把所有这些放在一个文件调用匹配器。运行它,输出如下:

> rspec -cfn matcher.rb 
MyObject
  should have value wes for attribute first
  should have value bailey for attribute last
  should have value 111.111.1111 for attribute phone
Finished in 0.00143 seconds
3 examples, 0 failures

我发现这个效果很好:

specify { @o.attribute.should eql(val) }
subject { @o }
it { attribute.should == value }

最新更新