2

構成で、モックする必要があるプロバイダーを使用するモジュールがあります。

プロバイダー自体にはregister、2 つの引数を受け取る$get関数とオブジェクトを返す関数がありますが、とにかくそれをモックするので、それはあまり重要ではありません (と思います)。念のため、プロバイダー全体を以下に示します

モジュールは次のようにプロバイダーを使用します。

angular.module('replenishment').config(configureReplenishmentColumnDef);

configureReplenishmentColumnDef.$inject = ['columnDefProvider'];
function configureReplenishmentColumnDef(columnDefProvider) {
   let config = {
      matchWhen: ((schema)=> _.get(schema, 'format') === 'replenishment'),
      $get: replenishmentColDef
   };

   columnDefProvider.register(config);
}

replenishmentColDef.$inject = ['$q', 'schema', 'medData'];
function replenishmentColDef($q, schema, medData) { 
  ....
}

このような仕様をまとめ始めました(CoffeeScript で記述されたテスト)

describe 'replenishment-module', ->

  columnDefProvider = undefined

  beforeEach ->
    module ($provide)->
      $provide.provider 'columnDef', ->
         @.register = sinon.spy()
         @.$get = sinon.spy()
         return // had to put an explicit return here, as @DTing suggested    

    module 'replenishment'

    inject ->

プロバイダーのメソッドを適切にモックする方法がわかりません。スパイの代わりにスタブを使用する必要があるかもしれません。

4

1 に答える 1

1

次のように明示的な戻り値を追加してみてください。不正な関数を取得していると思います。

describe 'replenishment-module', ->

  columnDefProvider = undefined

  beforeEach ->
    module ($provide)->
      $provide.provider 'columnDef', ->
        @.register = sinon.spy()
        @.$get = sinon.spy()
        return        

    module 'replenishment'

    inject ->

私はこれがあなたのjavascriptの同等物がどのように見えるかだと思います:

beforeEach(function() {
  module(function($provide) {
    return $provide.provider('columnDef', function() {
      this.register = sinon.spy();
      return this.$get = sinon.spy();
    });
  });

これにより、プロバイダーの columnDef に $get ファクトリ メソッドがなくなります。

function Something() {
  return this.get = function() {
    console.log("get");
  };
}

var something = new Something();
something.get();

于 2015-06-11T01:46:00.367 に答える