Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rspec测试记录在日期范围内_Ruby On Rails_Rspec - Fatal编程技术网

Ruby on rails Rspec测试记录在日期范围内

Ruby on rails Rspec测试记录在日期范围内,ruby-on-rails,rspec,Ruby On Rails,Rspec,我的模型中有这个作用域函数 # models/Post.rb def self.filtered (params) unless params[:year].blank? && params[:month].blank? year = params[:year].to_i month = params[:month].to_i return where(created_at: DateTime.new(year, month, 1

我的模型中有这个作用域函数

# models/Post.rb
  def self.filtered (params)

    unless params[:year].blank? && params[:month].blank?
      year = params[:year].to_i
      month = params[:month].to_i
      return where(created_at: DateTime.new(year, month, 1).beginning_of_day..DateTime.new(year, month, -1).end_of_day)
    end

    self

  end

# controllers/posts_controller.rb
@posts = Post.filtered(params)
它基本上返回特定年份和月份的所有存档帖子

SELECT  `posts`.* FROM `posts` 
WHERE (`posts`.`created_at` BETWEEN '2017-10-01 00:00:00' AND '2017-10-31 23:59:59')
我正在尝试为这个方法编写一个测试,以确保在请求的年份和月份创建了一个帖子,我如何才能做到这一点

# spec/models/post_spec.rb
  describe '.filtered' do
    let!(:older) { FactoryGirl.create(:post, created_at: 1.month.ago) } # this post should not appear in the list
    let!(:newer) { FactoryGirl.create(:post, created_at: Time.zone.now) } # this post should appear in the list

    it 'is within specific year and month' do
      expect(Post.filtered({year: Date.today.strftime("%Y"), month: Date.today.strftime("%m")}).map { |post| post.created_at }).to be ???

    end
  end
使用验证结果集中是否包含记录

expect(Post.filtered({year: Date.today.strftime("%Y"), month: Date.today.strftime("%m")}).to include(newer)
使用验证结果集中是否包含记录

expect(Post.filtered({year: Date.today.strftime("%Y"), month: Date.today.strftime("%m")}).to include(newer)
当应忽略顺序时,使用
#contain_justice
匹配元素

# spec/models/post_spec.rb
  describe '.filtered' do
    let!(:older) { FactoryGirl.create(:post, created_at: 1.month.ago) } # this post should not appear in the list
    let!(:newer) { FactoryGirl.create(:post, created_at: Time.zone.now) } # this post should appear in the list

    it 'is within specific year and month' do
      expect(Post.filtered({year: Date.today.strftime("%Y"), month: Date.today.strftime("%m")}).map { |post| article.created_at }).to contain_exactly(newer)

end
结束

顺便说一下,不是创建一个类方法,就像你在这里所做的那样,你可能想考虑一个范围,以便它可以与其他范围链接。

< P>使用“代码> *包容”精确< <代码>来匹配元素,当忽略顺序时。

# spec/models/post_spec.rb
  describe '.filtered' do
    let!(:older) { FactoryGirl.create(:post, created_at: 1.month.ago) } # this post should not appear in the list
    let!(:newer) { FactoryGirl.create(:post, created_at: Time.zone.now) } # this post should appear in the list

    it 'is within specific year and month' do
      expect(Post.filtered({year: Date.today.strftime("%Y"), month: Date.today.strftime("%m")}).map { |post| article.created_at }).to contain_exactly(newer)

end
结束


顺便说一下,不是创建一个类方法,就像你在这里所做的那样,你可能想考虑一个范围,以便它可以与其他范围链接。

有一种方法可以在一个期望中写入这个代码>()。基本上是在一个expect中编写而不是在两个expect中编写有没有一种方法可以在一个expect中编写这个
expect()。包含(更新的)。不包含(旧的)
?基本上是一次完成,而不是两次完成