Activerecord 失败/错误:get:index nomethoder错误:未定义的方法`<=';零级:零级
我希望这不是一个具体的错误,它可能是一个逻辑错误,也可以帮助其他人 我想测试我的控制器索引方法,我有一个带有活动记录查询的before方法。问题是我无法找出我的测试出了什么问题,因为我只有在使用rspec时才得到这个错误,而不是在手动测试它时 控制器 测试输出Activerecord 失败/错误:get:index nomethoder错误:未定义的方法`<=';零级:零级,activerecord,rspec,ruby-on-rails-3.2,Activerecord,Rspec,Ruby On Rails 3.2,我希望这不是一个具体的错误,它可能是一个逻辑错误,也可以帮助其他人 我想测试我的控制器索引方法,我有一个带有活动记录查询的before方法。问题是我无法找出我的测试出了什么问题,因为我只有在使用rspec时才得到这个错误,而不是在手动测试它时 控制器 测试输出 1)BlogsController GET#index显示所有博客文章 失败/错误:获取:索引 命名错误: 未定义方法`测试数据库中似乎有一个或多个Blog对象没有publish\u date属性集。这就是为什么。选择{| p | p.p
1)BlogsController GET#index显示所有博客文章
失败/错误:获取:索引
命名错误:
未定义方法`测试数据库中似乎有一个或多个Blog
对象没有publish\u date
属性集。这就是为什么。选择{| p | p.publish|u date此问题的修复程序将在每次测试后清理数据库。例如:
class BlogsController < ApplicationController
before_filter :archive_months
def archive_months
@months = Blog.find(:all, :select=>:publish_date, :order=>"publish_date DESC")
.select{ |p| p.publish_date <= Date.today }
.group_by{ |p| [p.year, p.month, p.publish_date.strftime("%B")] }
end
require 'rails_helper'
RSpec.describe BlogsController, :type => :controller do
before(:each) do
@post1 = create(:blog)
end
describe "GET #index" do
it "shows all blog posts" do
get :index
expect(response).to have_http_status(200)
end
end
end
1) BlogsController GET #index shows all blog posts
Failure/Error: get :index
NoMethodError:
undefined method `<=' for nil:NilClass
# ./app/controllers/blogs_controller.rb:178:in `block in archive_months'
# ./app/controllers/blogs_controller.rb:177:in `select'
# ./app/controllers/blogs_controller.rb:177:in `archive_months'