如何测试我的模型方法Project.all()?

时间:2014-08-10 13:39:01

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

如何测试我的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

我的问题是我检查它以通过此测试

1 个答案:

答案 0 :(得分: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