gpt4 book ai didi

unit-testing - 在 Browserify 中为单元测试 stub 所需的模块

转载 作者:行者123 更新时间:2023-12-04 04:50:03 28 4
gpt4 key购买 nike

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

所以我有以下

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()

虽然这确实有效,但感觉有点讨厌,因为我不得不向我的脚本添加代码只是为了运行测试。

有谁知道更好的方法?

最佳答案

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

关于unit-testing - 在 Browserify 中为单元测试 stub 所需的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22374270/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com