我有以下观点:
...
var TreeView = Backbone.View.extend({
el: '#org-tree',
initialize: function() {
eventBus.on("route:change", _.bind(this.triggerFilterEvent, this));
},
render: function() { ... },
foo: function() { console.log("foo"); },
triggerFilterEvent: function(name) {
this.foo();
...
}
});
...
Run Code Online (Sandbox Code Playgroud)
我的规格如下:
describe('TreeView', function() {
var treeView = new TreeView();
it('calls triggerFilterEvent when receiving a route:change', function() {
spyOn(treeView, 'triggerFilterEvent');
spyOn(treeView, 'foo');
treeView.delegateEvents();
eventBus.trigger("route:change", "test");
console.log('TriggerCOunt:' + treeView.triggerFilterEvent.callCount);
console.log('FooCount: ' + treeView.foo.callCount);
expect(treeView.triggerFilterEvent).toHaveBeenCalled();
});
});
Run Code Online (Sandbox Code Playgroud)
我treeView.delegateEvents()按照以下解决方案的建议添加:
SpyOn使用jasmine的骨干视图方法
但是我的测试仍然失败了:
LOG: 'triggerFilterEvent with: test'
LOG: 'Event has been …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的项目转换为groovy,并希望继续使用JUnit4和Mockito.我有一个测试,当我调用不同的方法时,它会验证是否会调用一个spring aop around advice.
这是我的代码:
@Test
void testPointCut() {
//Given
def target = new MainController();
def factory = new AspectJProxyFactory(target);
def aspect = mock(LoggingAspect.class);
factory.addAspect(aspect);
def proxy = factory.getProxy();
when(aspect.log(any(ProceedingJoinPoint))).thenReturn(null);
//When
proxy.index();
//Then
verify(aspect).log(any(ProceedingJoinPoint));
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到一个例外:
testPointCut(com.meetupinthemiddle.LoggingAspectTest) Time elapsed: 0.277 sec <<< ERROR!
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
0 matchers expected, 1 recorded:
-> at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be …Run Code Online (Sandbox Code Playgroud) 从CUT被测类中摘录:
def compileOutputLines( TopDocs topDocs ) {
println "gubbins"
}
Run Code Online (Sandbox Code Playgroud)
测试代码:
def "my feature"(){
given:
CUT stubCut = Stub( CUT ){
compileOutputLines(_) >> { TopDocs mockTD ->
// NB no Exception is thrown
// try {
println "babbles"
callRealMethod()
println "bubbles"
// }catch( Exception e ) {
// println "exception $e"
// }
}
}
CUT spyCut = Spy( CUT ){
compileOutputLines(_) >> { TopDocs mockTD ->
println "babbles 2"
callRealMethod()
println "bubbles 2"
}
}
when:
stubCut.compileOutputLines( …Run Code Online (Sandbox Code Playgroud) 我在测试用例文件中模拟了一个函数。
MyService.getConfigsForEntity = jest.fn().mockReturnValue(
new Promise(resolve => {
let response = [];
resolve(response);
})
);
Run Code Online (Sandbox Code Playgroud)
现在我希望该文件中的所有测试用例都使用这个模拟
describe('Should render Component Page', () => {
it('should call the API', async () => {
const {container} = render(<MyComp entityName='entity1'/>);
await wait(() => expect(MyService.getConfigsForEntity).toHaveBeenCalled());
});
});
Run Code Online (Sandbox Code Playgroud)
唯一的问题是只有一个测试用例我想以不同的方式模拟返回值。
但之前和之后的所有其他测试用例都可以使用全局模拟。
describe('Should call API on link click', () => {
it('should call the API on link click', async () => {
const spy = jest.spyOn(MyService, 'getConfigsForEntity ').mockReturnValue(
new Promise(resolve => {
let response = [{
"itemName": "Dummy" …Run Code Online (Sandbox Code Playgroud) 我想要做的是创建一个可以在没有用户交互的情况下执行它的功能的应用程序.这不应该在Device中的Applications页面上有任何appicon.安装后,用户无需知道设备中运行的应用程序.我在演示应用程序中尝试使用No Launcher Activity但它没有运行应用程序代码,这很明显.有没有办法完成这项任务,这有什么意义吗?