【问题标题】:Converting Rspec mocks to Mocha for testing active-model-serializers将 Rspec 模拟转换为 Mocha 以测试活动模型序列化器
【发布时间】:2013-06-01 10:37:01
【问题描述】:

我正在使用 Benedikt Diecke 关于自定义 rspec 示例组的帖子来测试活动模型序列化程序,但无法将其转换为使用 mocha 而不是 rspec 模拟。

http://benediktdeicke.com/2013/01/custom-rspec-example-groups/

该示例包含一个模拟将被序列化的模型类的通用方法,它使用 rspec 模拟 - 我如何将其转换为使用 mocha?

let(:resource) do
    double(resource_name, attributes).tap do |double|
        double.stub(:read_attribute_for_serialization) { |name| attributes[name] }
    end
end

【问题讨论】:

    标签: testing rspec mocking active-model-serializers


    【解决方案1】:

    我看不出在那里使用模拟有很多好处。我会从资源工厂方法返回一个真实的实例,即在自定义示例组中根本不定义 let(:attributes) 而只是定义

    let(:resource) do
      {}
    end
    

    然后你的序列化规范看起来像

    require 'spec_helper'
    
    describe UserSerializer do
      let(:resource){ FactoryGirl.build(:resource_name) }
    
      it { should have_key(:name) }
      it { should have_key(:email) }
      it { should have_key(:created_at) }
      it { should have_key(:updated_at) }
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-04
      • 2018-01-16
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 2018-09-01
      • 1970-01-01
      相关资源
      最近更新 更多