【问题标题】:Stubbing required module in Browserify for Unit Testing在 Browserify 中为单元测试存根所需的模块
【发布时间】:2014-04-17 22:19:45
【问题描述】:

好的,我刚刚开始使用 Browserify,并且在编写我的 jasmine 测试时遇到了一些问题。基本上我有一个主干视图,其中包含嵌套视图,现在出于测试目的,我显然不想在这里测试嵌套视图,因为它正在使用自己的单元测试进行测试。我想要做的只是把它存根,检查它是否被正确调用

所以我有以下

nestedView  = require('./nested_view.coffee')

module.exports = class MainView extends Backbone.View
   initialize:(collection, attr)->
     NestedView = if _.isUndefined(attr.Stub) then NestedView else attr.Stub
     @nested_view = new NestedView()

然后在我的测试中:

it "Tests my nested view", ->
  Stub =  sinon.stub()
  @mainview  = new MainView(collection:@collection, {SV:Stub})
  expect(Stub).toHaveBeenCalledOnce()

虽然这确实有效,但感觉有点讨厌,因为我不得不将代码添加到我的脚本中以运行测试。

有人知道更好的方法吗?

【问题讨论】:

标签: unit-testing jasmine sinon browserify


【解决方案1】:

您应该查看 rewireify 或 proxyquireify。它们将允许您在测试中操纵“nestedView = require('./nested_view.coffee')”语句。但是,是的,您必须为要模拟的每个 require 语句执行此操作。您可能还想查看 Facebook 的 Jest,它会自动为您模拟所有 require 语句。

【讨论】:

猜你喜欢
  • 2023-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 2012-03-22
  • 1970-01-01
  • 2011-07-02
  • 1970-01-01
相关资源
最近更新 更多