我有一个要测试的文件,其中有一个class ViewController: UIViewController {
override func viewDidLoad() {
let customSearchBar = CustomSearchBar.instantiateView()
customSearchBar.translatesAutoresizingMaskIntoConstraints = false
customSearchBar.txtSearch.delegate = self
self.view.addSubview(customSearchBar)
customSearchBar.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 0).isActive = true
customSearchBar.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: 0).isActive = true
customSearchBar.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
customSearchBar.heightAnchor.constraint(equalToConstant: 60).isActive = true
}
}
extension ViewController: UITextFieldDelegate {
func textFieldDidEndEditing(_ textField: UITextField) {
}
//------------------------------------------------------------------------------
func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {
}
}
应该被嘲笑:
http-client
:
schema.js
我需要模拟const { http } = require('@foo/http-client');
.......
const result = await http.foo('another-service').get('graphql', {
query: `
{
SomeResolver(clientId: "${args.clientId}") {
id
start
end
}
}
`,
});
。
result
:
schema.test.js
const sinon = require('sinon');
const mockHttp = sinon.mock(require('@foo/http-client'));
.........
mockHttp.expects('foo').returns({
get: (x,y) => {
// mock the response here
},
});
是上述错误,因为TypeError: Attempted to wrap undefined property foo as function
是http
。
但是,如果我将destructured
行更改为:
expects
我收到错误mockHttp.expects('http').returns
,这也很有意义,因为TypeError: Attempted to wrap object property http as function
是http
。
您可以看到,我对property
还是陌生的,但是我的问题是,当Sinon
是http.foo.get
时如何嘲笑http
?
答案 0 :(得分:1)
据我所知,模拟对象是对象,期望对象是功能-每个对象一个。
在您的情况下,您似乎可以做到:
const { http } = require('@foo/http-client');
const mockHttp = sinon.mock(require('@foo/http-client').http); // mind .http
mockHttp.expects('foo').returns({
get: (x,y) => {
// mock the response here
},
});
http.foo().get();
mockHttp.verify();
如果您需要对其他http
函数设置期望,则可以重用mockHttp
。如果您需要对模块中其他地方的函数有期望,则需要为声明它们的对象创建另一个模拟,并verify()
也创建另一个模拟。 (无论如何,这就是我的理解,如果您更了解,请有人纠正!)
在任何情况下,使用解构分配都不会改变您的http
变量引用模块的http
属性的事实。换句话说:
const { http } = require('@foo/http-client');
console.log( http === require('@foo/http-client').http ); // true