如何编写用于从模拟数据创建实例的模型的rspec测试用例

时间:2018-10-05 15:54:04

标签: ruby rspec

我在mock.rb中有以下模拟

class Mock
  def self.mock_data
    {
      test: [
              {id: 'abc'},
              {startDate: '2018-01-01'},
              {endDate: '2018-01-01'},
              {productIds: [ { id: 1 }, { id: 2} ]}
      ]
    }
  end
end

此模拟来自其他服务。

Model.rb

class Model
  include ActiveModel::Model

  attr_accessor :id, :startDate, :endDate, :productIds

  def self.find
     get_mock = Mock.mock_data.to_json
     final_mock = JSON.parse(get_mock, object_class: OpenStruct)
    Model.new (
      ...loop over the hash and build new instance of class....
    )
  end

我为此写了几张rspec。

model_spec.rb

expect { Mock.mock_data }.to_not raise_error

allow(Mock).to receive(:mock_data).and_return(HASH) <-在rspec中创建了一个模拟哈希

expect { Model.find }.to_not raise_error

如何为model.rb编写测试用例?

谢谢

0 个答案:

没有答案
相关问题