【问题标题】:How to test my model method Project.all()?如何测试我的模型方法 Project.all()?
【发布时间】:2014-08-10 13:39:00
【问题描述】:

如何测试我的model 方法Project.all()。当我查询我的方法时,我得到一个Projects 的列表,但我不确定要针对它发什么短信?

require 'spec_helper'

describe Project do

  before(:all) do

  end

  it "get all projects" do 
    projects = Project.all( authorization: @token)
  end

end

我的问题是我要通过什么检查才能通过此测试

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 unit-testing rspec


    【解决方案1】:

    您可以执行以下操作:-

    describe Project do
      # use the mandatory attributes while creating the Project objects.
      # I assumed, there is a :name attribute defined. But this is an idea
      # about the approach.
    
      let!(:proj1) { Project.create(name: "a", authorization: @token) }
      let!(:proj2) { Project.create(name: "b", authorization: @token) }
      let!(:proj3) { Project.create(name: "c", authorization: @token1) }
    
      after(:all) { Project.delete_all }
    
      it "get all projects" do 
        expect(Project.all( authorization: @token)).to match_array([proj1, proj2])
      end
    end
    

    【讨论】:

    • 如果用户已经包含一些项目怎么办,因为我正在使用基于休息的服务......所以无法控制。
    • @Mathew 你为什么要访问原始数据库。这就是我们不测试的方式。
    • after 块有什么作用?
    • 默认是 after(:each) 或 after(:all)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    相关资源
    最近更新 更多