我试图单元测试这一类,它具有的AppDB
和createStudy
,我需要模拟的依赖。要开始我试图单元测试简单的方法startLoadingData
这恰好是一个MobX action
玩笑单元类使用依赖
import { observable, action } from 'mobx'
import { Intent } from '@blueprintjs/core'
import { createStudy } from '../database/DatabaseInit'
import { AppDB } from '../database/Database'
export default class UIStore {
// ui state
// booleans indicating open/close state of modals
@observable createDialogue
@observable importDialogue
@observable revisionsDialogue
@observable runDialogue
// boolean indicating loading or waiting for async action
@observable loadingData
// array indicating navigation
@observable breadcrumbs
@observable processingMessages
constructor(rootStore) {
this.rootStore = rootStore
this.breadcrumbs = []
this.importDialogue = false
this.createDialogue = false
this.revisionsDialogue = false
this.runDialogue = false
// boolean to display loading blur on table that displays data
this.loadingData = false
// processing messages for import and other async loads
this.processingMessages = []
}
@action startLoadingData() {
this.loadingData = true
}
}
我的测试文件下面是毫无进展,因为有被抛出相关的sqlite3
在一个单独依赖一个错误AppDB
和createStudy
进口。我的理解是,如果我嘲笑这两个依赖,我可避免的错误,因为他们会被嘲笑,而不是真正的实现欲以sqlite3
。
// UIStore domain store unit test
// import * as Database from '../../app/database/Database'
// import * as DatabaseInit from '../../app/database/DatabaseInit'
import UIStore from '../../app/stores/UIStore'
describe('UIStore',() => {
beforeEach(() => {
// jest.spyOn(Database, 'AppDB').andReturn('mockAppDB')
// jest.spyOn(DatabaseInit, 'createStudy').andReturn('createStudy')
jest.mock('../../app/database/Database')
// jest.mock('DatabaseInit')
})
it('starts loading data',() => {
const testUIStore = new UIStore(this)
testUIStore.startLoadingData()
expect(testUIStore.loadingData).toBe(true)
})
})
正如你所看到的,尝试一堆东西,但我似乎没有得到任何地方。我读过有关手动嘲笑,以为所以我做的Database
但甚至不知道人工模拟如果我正确地这样做,可能是这种情况。
const Database = jest.genMockFromModule('../Database.js')
module.exports = Database
我不认为这个问题,但它可能是值得一提的是AppDB
是一个ES6类createStudy
是一种方法。
我已经使用下面的代码实现了该文件结构: 'const sqlite3 = jest.genMockFromModule('sqlite3'); module.exports = sqlite3; ' 基本上没有我不必嘲笑具体方法,我只是不希望它的实际'sqlite3'节点模块中寻找,因为由于我的项目环境(电子),它是用玩笑(节点)不兼容。 然而,似乎即使有这样的模拟,实际'sqlite3'模块仍然被引用。 – nschwan94
看来我已经解决我自己的问题,是由于起源于我的电子应用的两个'package.json'结构,那'node_modules'在我的'app'目录并没有因为这是在节点的绑定我的根目录。一个简单的绑定副本消除了我得到的错误,并消除了我应该简单地嘲笑库的理论。对不起,这个问题实际上比看起来更具体。但是,我认为原则上,根据我读过的关于Jest的其他所有内容,你的答案是有意义的。 – nschwan94