【问题标题】:Ruby on Rails: How to validate data using Rspec or cucumberRuby on Rails:如何使用 Rspec 或 cucumber 验证数据
【发布时间】:2013-04-04 10:56:01
【问题描述】:

我正在 Homepage01 中的 HotDeal 模型中查询数据,该模型存在于 ProductDetailController 中,如下所示

def Homepage01
    @hotdetail=HotDeal.where("department_id=?",1).limit(15).offset(0)
end

我需要使用 Rspec 测试或验证数据。如何验证 RSpec 或 Cucumber 框架中的数据或任何 mysql 错误?

【问题讨论】:

  • 验证它是什么意思?模型的验证也在测试环境中运行,如果有 mysql 错误将被抛出并显示在控制台中。您能否详细说明您真正想要实现的目标?
  • 我将在控制器中创建一个 HotDeal.where("department_id=?",1).limit(15).offset(0) 的 mysql 查询,因此我必须使用这个结果集进行测试使用 Ruby on rails 测试框架是否得到了预期的结果或任何错误

标签: ruby-on-rails ruby rspec cucumber rspec-rails


【解决方案1】:

使用factory girldepartment_id 1 创建一个hotdeal factory。

在你的 product_detail_controller_spec.rb 里面(顺便说一句,应该是复数,即你的控制器和你的规范的 product_details)写:

require 'spec_helper'

describe ClientsController do
  describe "GET Homepage01" do
    it "assigns the right records" do
      FactoryGirl.create(:hot_deal)
      hotdetail = HotDeal.where("department_id=?",1).limit(15).offset(0)
      get "Homepage01"
      assigns(:hotdetail).should eq(hotdetail)
    end
  end
end

这将确保您的查询 1)在技术上和 2)按预期工作。

【讨论】:

  • assigns(:hotdetail).should eq(hotdetail) am not getting this line 你能解释一下吗?
  • assigns(hotdetail) 确保您的控制器分配一个名为 @hotdetail 的实例变量,然后确保它等于 hotdetail,因为这是您使用查询的地方。
  • 我遵循了你的代码,但我得到了 #<:core::examplegroup::nested_1::nested_1:0xf405358> 的未定义方法 `assigns'
  • 您似乎需要渲染一些东西才能使用assigns 方法 - 您是否在该控制器方法之后渲染了一些东西?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多