RSpec - Date应该在两个日期之间。

时间:2022-06-19 14:18:19

How can I test a date to see if it's between two dates? I know I can do two greater-than and less-than comparisons but I want an RSpec method to check the "betweeness" of the date.

我如何测试一个日期,看它是否在两个日期之间?我知道我可以做两件事——比比较少,但我想要一个RSpec方法来检查日期的“中间”。

For example:

例如:

it "is between the time range" do
    expect(Date.now).to be_between(Date.yesterday, Date.tomorrow)
end

I tried expect(range).to cover(subject) but no luck.

我试着期望(范围)。涵盖(主题)但没有运气。

4 个解决方案

#1


21  

Date.today.should be_between(Date.today - 1.day, Date.today + 1.day)

Date.today。应该be_between(日期。今天- 1。天,日期。今天+ 1.天)

#2


9  

Both of the syntaxes you wrote are correct RSpec:

你写的两个语法都是正确的RSpec:

it 'is between the time range' do
  expect(Date.today).to be_between(Date.yesterday, Date.tomorrow)
end

it 'is between the time range' do
  expect(Date.yesterday..Date.tomorrow).to cover Date.today
end

If you are not using Rails you won't have Date::yesterday or Date::tomorrow defined. You'll need to manually adjust it:

如果您不使用Rails,您将没有日期::yesterday或Date:::tomorrow defined。您需要手动调整:

it 'is between the time range' do
  expect(Date.today).to be_between(Date.today - 1, Date.today + 1)
end

The first version works due to RSpec's built in predicate matcher. This matcher understand methods being defined on objects, and delegates to them as well as a possible ? version. For Date, the predicate Date#between? comes from including Comparable (see link).

第一个版本由于RSpec内置的谓词匹配器而工作。这个匹配器理解在对象上定义的方法,并将它们委托给它们。的版本。对于Date,谓词Date#between?来自包括可比较的(见链接)。

The second version works because RSpec defines the cover matcher.

第二个版本可以工作,因为RSpec定义了封面匹配程序。

#3


2  

I didn't try it myself, but according to this you should use it a bit differently:

我自己没试过,但根据这个,你应该有一点不同:

it "is between the time range" do    
  (Date.yesterday..Date.tomorrow).should cover(Date.now)
end

#4


1  

You have to define a matcher, check https://github.com/dchelimsky/rspec/wiki/Custom-Matchers

您必须定义一个matcher,请检查https://github.com/dchelimsky/rspec/wiki/custommatchers

It could be

它可能是

RSpec::Matchers.define :be_between do |expected|
  match do |actual|
    actual[:bottom] <= expected && actual[:top] >= expected
  end
end

It allows you

它允许您

it "is between the time range" do
    expect(Date.now).to be_between(:bottom => Date.yesterday, :top => Date.tomorrow)
end

#1


21  

Date.today.should be_between(Date.today - 1.day, Date.today + 1.day)

Date.today。应该be_between(日期。今天- 1。天,日期。今天+ 1.天)

#2


9  

Both of the syntaxes you wrote are correct RSpec:

你写的两个语法都是正确的RSpec:

it 'is between the time range' do
  expect(Date.today).to be_between(Date.yesterday, Date.tomorrow)
end

it 'is between the time range' do
  expect(Date.yesterday..Date.tomorrow).to cover Date.today
end

If you are not using Rails you won't have Date::yesterday or Date::tomorrow defined. You'll need to manually adjust it:

如果您不使用Rails,您将没有日期::yesterday或Date:::tomorrow defined。您需要手动调整:

it 'is between the time range' do
  expect(Date.today).to be_between(Date.today - 1, Date.today + 1)
end

The first version works due to RSpec's built in predicate matcher. This matcher understand methods being defined on objects, and delegates to them as well as a possible ? version. For Date, the predicate Date#between? comes from including Comparable (see link).

第一个版本由于RSpec内置的谓词匹配器而工作。这个匹配器理解在对象上定义的方法,并将它们委托给它们。的版本。对于Date,谓词Date#between?来自包括可比较的(见链接)。

The second version works because RSpec defines the cover matcher.

第二个版本可以工作,因为RSpec定义了封面匹配程序。

#3


2  

I didn't try it myself, but according to this you should use it a bit differently:

我自己没试过,但根据这个,你应该有一点不同:

it "is between the time range" do    
  (Date.yesterday..Date.tomorrow).should cover(Date.now)
end

#4


1  

You have to define a matcher, check https://github.com/dchelimsky/rspec/wiki/Custom-Matchers

您必须定义一个matcher,请检查https://github.com/dchelimsky/rspec/wiki/custommatchers

It could be

它可能是

RSpec::Matchers.define :be_between do |expected|
  match do |actual|
    actual[:bottom] <= expected && actual[:top] >= expected
  end
end

It allows you

它允许您

it "is between the time range" do
    expect(Date.now).to be_between(:bottom => Date.yesterday, :top => Date.tomorrow)
end