我试图窥探$ timeout,以便我可以验证它没有被调用.具体来说,我的生产代码(见下文)将$ timeout称为函数,而不是对象:
$timeout(function() { ... })
Run Code Online (Sandbox Code Playgroud)
并不是
$timeout.cancel() // for instance
Run Code Online (Sandbox Code Playgroud)
然而,Jasmine需要一个物体被监视,如下所示:
spyOn(someObject, '$timeout')
Run Code Online (Sandbox Code Playgroud)
我不知道'someObject'会是什么.
我正在使用Angular模拟,如果这有任何区别.
编辑:我正在尝试测试的相关生产代码如下所示:
EventHandler.prototype._updateDurationInOneSecondOn = function (call) {
var _this = this;
var _updateDurationPromise = this._$timeout(function () {
call.duration = new Date().getTime() - call.startTime;
_this._updateDurationInOneSecondOn(call);
}, 1000);
// ... more irrelevant code
}
Run Code Online (Sandbox Code Playgroud)
在特定的测试场景中,我试图断言永远不会调用$ timeout.
编辑2:明确指出我使用$ timeout作为函数,而不是对象.
'use strict'
webApp.controller 'NavigationController', [
'$scope'
'$rootScope'
'UserService'
($scope, $rootScope, UserService) ->
$scope.init = ->
UserService.isAuthenticated().then (authenticated) ->
$scope.isAuthenticated = authenticated
$scope.init()
]
Run Code Online (Sandbox Code Playgroud)
我想写一个测试,spyOn如果isAuthenticated被调用UserService.在我beforeEach,我有:
beforeEach ->
module 'webApp'
inject ($injector) ->
$httpBackend = $injector.get '$httpBackend'
$q = $injector.get '$q'
$rootScope = $injector.get '$rootScope'
$scope = $rootScope.$new()
$controller = $injector.get '$controller'
UserServiceMock =
isAuthenticated: ->
deferred = $q.defer()
deferred.promise
controller = $controller 'AboutUsController',
'$scope': $scope
'$rootScope': $rootScope
'UserService': UserServiceMock
$httpBackend.whenGET('/api/v1/session').respond 200
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激..谢谢
我有一个叫做的类Availability.java,有两种方法.
public Long getStockLevelStage() {
//some logic
getStockLevelLimit();
}
public Long getStockLevelLimit() {
String primaryOnlineArea = classificationFeatureHelper.getFirstFeatureName(productModel, FEATURE_CODE_PRODUCT_ONLINE_AREA_PRIMARY, language);
................
return new Long();
}
Run Code Online (Sandbox Code Playgroud)
我正在写一个单元测试课AvailabilityTest.java.
@RunWith(MockitoJUnitRunner.class)
public class AvailabilityTest {
@InjectMocks
private Availability availability = new Availability();
@Test
public void testGetStockLevelStage() {
availability.getStockLevelStage();
}
}
Run Code Online (Sandbox Code Playgroud)
当我调用availability.getStockLevelStage()方法时,它调用getStockLevelLimit()方法.是否可以模拟内部方法调用?
在这种情况下,我不希望getStockLevelLimit()在执行时getStockLevelStage()执行.
请帮忙.
我希望能够在 Mockito Spy 对象上单步调试调试器,尤其是 Eclipse。例如,假设我有以下课程要测试:
public class someClass(){
public someMethod(){
System.out.println("Begin someMethod call");
//some code in the method
System.out.println("End someMethod call");
}
}
Run Code Online (Sandbox Code Playgroud)
假设我有一个 Mockito 类,如下所示:
@RunWith(MockitoJUnitRunnerclass)
public class SomeMethodTest {
@Spy SomeClass someSpiedObject;
@Test
public final void testSomeMethod(){
//some code in the test method
someSpiedObject.someMethod();
/some tests
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,如果我在测试方法中的某些代码和方法中的某些代码处设置调试断点,将到达第一个断点,但不会到达第二个断点。我知道代码会运行,因为系统输出会打印到控制台。我知道,作为一个被监视的对象,它不是相同的代码,调试器不应该工作。我也知道,如果 VerboseLogging() 是 Mock 对象而不是 Spy,则可以使用它。我真正的问题是,如何像执行普通对象一样逐步执行代码?是否有可以帮助的调试框架?是否有我不知道的 Mockito 功能?我最终将不再需要它是一个 Spy 对象并且可以简单地测试它,但直到静态调用被重构。
我正在努力使用间谍On作为测试我的utils.js模块的一部分。我尝试了各种方法和途径,但似乎都产生了“预期的模拟函数被调用”。根据记录,其他单元测试工作正常,因此我的实际测试设置不应该有任何问题。
下面是一个简化的测试用例,包含两个函数和一个测试,我什至无法让它们工作。我是否完全误解了间谍?
// utils.js
function capitalHelper(string){
return string.toUpperCase();
}
function getCapitalName(inputString){
return capitalHelper(inputString.charAt(0)) + inputString.slice(1);
}
exports.capitalHelper = capitalHelper
exports.getCapitalName = getCapitalName
// utils.test.js
const Utils = require('./utils');
test('helper function was called', () => {
const capitalHelperSpy = jest.spyOn(Utils, 'capitalHelper');
const newString = Utils.getCapitalName('john');
expect(Utils.capitalHelper).toHaveBeenCalled();
})Run Code Online (Sandbox Code Playgroud)
有没有办法窥探 Golang 中的方法?
例如,假设我有
type Object struct {
A int
B string
C *interface{}
}
func (o *Object) Something(val interface{}) {
o.A = 102
// some other business logic under test
o.SomethingElse(o.C, val)
}
//...
func (o *Object) Process(val interface{}) interface{} {
// some business logic
return 43 // or something else. no me importa ya
}
//...
func (o *Object) SomethingElse(iPtr *interface{}, val interface{}) {
processedVal := o.Process(val)
if iPtr == nil {
iPtr = new(interface{})
}
*iPtr = …Run Code Online (Sandbox Code Playgroud) 在Jest中,为了监视(以及可选地模拟实现)方法,我们执行以下操作:
const childProcess = require('child_process');
const spySpawnSync = jest.spyOn(childProcess, 'spawnSync').mockImplementation();
Run Code Online (Sandbox Code Playgroud)
这允许我们spySpawnSync检查上次调用它时使用的参数,如下所示:
expect(spySpawnSync).lastCalledWith('ls');
Run Code Online (Sandbox Code Playgroud)
但是,对于导出函数的 Node 模块(例如execa包)来说,这是不可能的。
我尝试了以下各项,但没有一个监视或模拟该功能:
// Error: `Cannot spy the undefined property because it is not a function; undefined given instead`
jest.spyOn(execa);
// Error: `Cannot spyOn on a primitive value; string given`
jest.spyOn('execa');
// Error: If using `global.execa = require('execa')`, then does nothing. Otherwise, `Cannot spy the execa property because it is not a function; undefined given instead`.
jest.spyOn(global, 'execa');
Run Code Online (Sandbox Code Playgroud)
因此,是否有任何方法可以监视导出函数的模块,例如execa在给定的示例中?
有一个abstract班
public abstract class BaseProcessor {
public BooksTransaction getBooksTransaction() {
return booksTransaction;
}
}
Run Code Online (Sandbox Code Playgroud)
还有一个final class要使用Junit进行测试
public final class CreateOrganisationProcessor extends BaseProcessor {
public boolean process() throws Exception { //method to be tested
request = new CreateOrganisationRequest(IntegrationSystems.valueOf(getBooksTransaction().getSource()),
IntegrationSystems.valueOf(getBooksTransaction().getDestination()), getBooksTransaction());
request.setRequestTypes(getRequestTypes());
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试监视类BaseProcessor并模拟 getBooksTransaction对象的方法return BooksTransaction。
代码:
@Test
public void testProcess() throws Exception {
BaseProcessor spy = Mockito.spy(new CreateOrganisationProcessor());
BooksTransaction booksTransaction = new BooksTransaction();
booksTransaction.setReferenceID(DEFAULT_REFERENCE_ID);
Mockito.doReturn(booksTransaction).when(spy).getBooksTransaction(); …Run Code Online (Sandbox Code Playgroud) 我有一个 Angular 库,其中包含“导出函数 myFunction”等函数。
出现错误的项目将此库作为依赖项,当我想监视该函数时,会显示以下错误:
myFunction is not declared writable or has no setter
Run Code Online (Sandbox Code Playgroud)
现在是现实世界的例子:
库中的一个简单函数:
export function isNotEmptyString(value: any): value is string {
return _.isString(value) && !_.isEmpty(value);
}
Run Code Online (Sandbox Code Playgroud)
该函数打包后的dts:
export declare function isNotEmptyString(value: any): value is string;
Run Code Online (Sandbox Code Playgroud)
为了监视该函数,我必须在某个时刻拥有一个对象。
所以我使用自定义模块导入来实现这一点。
间谍错误:
import * as MyLibfrom 'my-lib';
const isNotEmptyStringSpy = spyOn(MyLib, 'isNotEmptyString').and.returnValue(false);
Run Code Online (Sandbox Code Playgroud)
错误是:
isNotEmptyString is not declared writable or has no setter
Run Code Online (Sandbox Code Playgroud)
现在,如果我使用spyOnPropety:
import * as MyLibfrom 'my-lib';
const isNotEmptyStringSpy = spyOnProperty(MyLib, 'isNotEmptyString').and.returnValue(() => false);
Run Code Online (Sandbox Code Playgroud)
新的错误是:
isNotEmptyString is …Run Code Online (Sandbox Code Playgroud) 这个问题并不是 Jest 特有的,因为它适用于所有具有存根功能的测试库。
\nESM 模块具有不可变的命名和默认导出,这意味着这不再有效:
\n// @filename foo.mjs\nexport function foo() { ... }\n\n// @filename foo.test.mjs\nimport * as foo from \'./foo.mjs\'\n// Causes runtime error because named export "foo" is immutable\njest.spyOn(foo, \'foo\')\nRun Code Online (Sandbox Code Playgroud)\n\xe2\x9d\x93使用 ESM 模块监视/模拟命名导出的当前“标准”方法是什么?
\n// @filename foo.mjs\nexport function foo() {\n return _private.foo()\n}\nexport const _private = {\n foo: () => { ... }\n}\n\n// @filename foo.test.mjs\nimport { _private } from \'./foo.mjs\'\njest.spyOn(_private, \'foo\')\nRun Code Online (Sandbox Code Playgroud)\n// @filename proxy.mjs\nexport function proxy(fn) {\n const functionProxy = …Run Code Online (Sandbox Code Playgroud) spy ×10
unit-testing ×6
jestjs ×3
mocking ×3
mockito ×3
angularjs ×2
jasmine ×2
java ×2
javascript ×2
angular ×1
debugging ×1
eclipse ×1
es6-modules ×1
function ×1
go ×1
junit ×1
junit4 ×1
karma-runner ×1
node.js ×1
stub ×1
typescript ×1