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

发布于 2024-11-01 17:26:41 字数 279 浏览 4 评论 0原文

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

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

如果我可以只进行一行测试,例如:

compare_value(ATTRIBUTE_NAME, VALUE)

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

I have a bunch of very repetitive rspec tests that all have the same format:

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

It would be nice if I could just make one line tests like:

compare_value(ATTRIBUTE_NAME, VALUE)

But shoulda doesn't seem to be geared toward these types of tests. Are there other alternatives?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

や莫失莫忘 2024-11-08 17:26:41

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

it { should do_something }

但是,这样的示例读起来不太好:

it { subject.attribute.should do_something }

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

specify { subject.attribute.should do_something }

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

Sometimes I regret exposing subject as an end-user device. It was introduced to support extensions (like shoulda matchers), so you can write examples like:

it { should do_something }

Examples like this, however, do not read well:

it { subject.attribute.should do_something }

If you're going to use subject explicitly, and then reference it explicitly in the example, I recommend using specify instead of it:

specify { subject.attribute.should do_something }

The underlying semantics are the same, but this ^^ can be read aloud.

昵称有卵用 2024-11-08 17:26:41

如果你希望它读起来更清楚并且只有 1 行,我会编写一个自定义的 RSpec 助手。假设我们要测试以下类:

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

如果将所有这些放入文件中,调用 matcher.rb 并运行它,则输出如下:

> 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

I would write a custom RSpec helper if you want it to read more clearly and be only 1 line. Suppose we have the following class we want to test:

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

We could write the following matcher:

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

Then to write the tests we could do something like:

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

If you put all of this in a file call matcher.rb and run it the following is output:

> 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
浪菊怪哟 2024-11-08 17:26:41

我发现这个效果很好:

specify { @o.attribute.should eql(val) }

I found this which works great:

specify { @o.attribute.should eql(val) }
℡Ms空城旧梦 2024-11-08 17:26:41
subject { @o }
it { attribute.should == value }
subject { @o }
it { attribute.should == value }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文