我不明白Spock测试中Mock,Stub和Spy之间的区别,我在网上看过的教程并没有详细解释它们.
Mockito - 我理解间谍调用对象上的真实方法,而模拟调用double对象上的方法.除非有代码气味,否则应避免使用间谍.但是,间谍如何工作以及我何时应该使用它们?他们与嘲笑有什么不同?
我想测试在我的Javascript对象构造函数中是否调用以下方法.从我在Jasmine文档中看到的,我可以窥探一个构造函数方法,并且我可以在实例化对象之后监视方法,但是在构造对象之前我似乎无法监视方法.
物体:
Klass = function() {
this.called_method();
};
Klass.prototype.called_method = function() {
//method to be called in the constructor.
}
Run Code Online (Sandbox Code Playgroud)
我想在规范中做这样的事情:
it('should spy on a method call within the constructor', function() {
spyOn(window, 'Klass');
var obj = new Klass();
expect(window.Klass.called_method).toHaveBeenCalled();
});
Run Code Online (Sandbox Code Playgroud) 我正在使用Jasmine来测试是否创建了某些对象并调用了它们的方法.
我有一个jQuery小部件,它创建flipcounter对象并调用它们的setValue方法.flipcounter的代码在这里:https://bitbucket.org/cnanney/apple-style-flip-counter/src/13fd00129a41/js/flipcounter.js
flipcounters使用以下方式创建:
var myFlipCounter = new flipCounter("counter", {inc: 23, pace: 500});
Run Code Online (Sandbox Code Playgroud)
我想测试创建flipcounters并调用setValue方法.我的问题是如何在创建这些对象之前监视这些对象?我是否会监视构造函数并返回虚假对象?示例代码确实会有所帮助.谢谢你的帮助!:)
更新:
我已经尝试过像这样监视flipCounter了:
myStub = jasmine.createSpy('myStub');
spyOn(window, 'flipCounter').andReturn(myStub);
//expectation
expect(window.flipCounter).toHaveBeenCalled();
Run Code Online (Sandbox Code Playgroud)
然后通过flipCounter测试setValue调用:
spyOn(myStub, 'setValue');
//expectation
expect(myStub.setValue).toHaveBeenCalled();
Run Code Online (Sandbox Code Playgroud)
初始化flipCounter的第一个测试很好,但是为了测试setValue调用,我得到的是'setValue()方法不存在'错误.我这样做是对的吗?谢谢!
我正在为我的 react 项目编写单元测试用例,并使用 jest 和酶编写测试用例。我已经阅读了 jest 文档
https://jestjs.io/docs/en/jest-object.html#jestspyonobject-methodname
这解释了jest.spyOn()
方法,但我没有完全理解。
所以我想知道更多关于我们应该使用的具体地方jest.fn()
和我们应该/必须使用的地方的更多细节jest.spyOn()
。如果可以用这两种方法的例子来解释,那将是一个很大的帮助。
谢谢
我正在使用Jasmine创建一个这样的间谍:
beforeEach(inject(function ($injector) {
$rootScope = $injector.get('$rootScope');
$state = $injector.get('$state');
$controller = $injector.get('$controller');
socket = new sockMock($rootScope);
//this is the line of interest
authService = jasmine.createSpyObj('authService', ['login', 'logout', 'currentUser']);
}));
Run Code Online (Sandbox Code Playgroud)
我希望能够改变各种方法返回的内容authService
.
以下是实际测试的设置方法:
function createController() {
return $controller('UserMatchingController', {'$scope': $rootScope, 'socket':socket, 'authService': authService });
}
describe('on initialization', function(){
it('socket should emit a match', function() {
createController();
expect(socket.emits['match'].length).toBe(1);
});
it('should transition to users.matched upon receiving matched', function(){
//this line fails with "TypeError: undefined is not a function"
authService.currentUser.andReturn('bob');
createController();
$state.expectTransitionTo('users.matched'); …
Run Code Online (Sandbox Code Playgroud) 我有一个JUnit类,使用不同的方法来执行不同的测试.
我使用Mockito在真实实例上创建一个间谍,然后覆盖一些与我执行的实际测试无关的方法.
有没有办法,只是为了清理我以防万一我的测试后运行的其他一些测试也使用相同的实例,可能会执行一个模拟的方法,他们没有要求模拟,取消模拟方法?
说我有一个名为'wareHouseSpy'的间谍对象
说我重写方法isSomethingMissing:
doReturn(false).when(wareHouseSpy).isSomethingMissing()
Run Code Online (Sandbox Code Playgroud)
什么是正确的方式取消覆盖,并使间谍恢复正常,即使下一个isSomethingMissing的调用运行真正的方法?
就像是
doReturn(Mockito.RETURN_REAL_METHOD).when(wareHouseSpy).isSomethingSpy()
Run Code Online (Sandbox Code Playgroud)
或者可能
Mockito.unmock(wareHouseSpy)
Run Code Online (Sandbox Code Playgroud)
谁知道?我在那个地方找不到任何东西
谢谢!
阿萨夫
我已经看到像spypig.com这样的服务在电子邮件中放置一个小图像,并在打开时从哪里跟踪.他们跟踪城市,国家,IP地址等.这是怎么做到的?
我正在使用Jasmine(2.2.0)间谍来查看是否调用了某个回调.
测试代码:
it('tests', function(done) {
var spy = jasmine.createSpy('mySpy');
objectUnderTest.someFunction(spy).then(function() {
expect(spy).toHaveBeenCalled();
done();
});
});
Run Code Online (Sandbox Code Playgroud)
这按预期工作.但现在,我正在增加第二个级别:
it('tests deeper', function(done) {
var spy = jasmine.createSpy('mySpy');
objectUnderTest.someFunction(spy).then(function() {
expect(spy).toHaveBeenCalled();
spy.reset();
return objectUnderTest.someFunction(spy);
}).then(function() {
expect(spy.toHaveBeenCalled());
expect(spy.callCount).toBe(1);
done();
});
});
Run Code Online (Sandbox Code Playgroud)
此测试永远不会返回,因为显然done
回调从未被调用过.如果我删除该行spy.reset()
,测试确实完成,但显然在最后的期望中失败.然而,这个callCount
领域似乎是undefined
,而不是2
.
我有一个骨干视图,我想创建一个测试来确认某个元素上的click事件将调用绑定到该元素的函数.我的观点是:
PromptView = Backbone.View.extend({
id:"promptPage",
attributes:{
"data-role":"page",
"data-theme":"a"
},
events:{
"click #btnYes": "answerYes",
"tap #btnYes": "answerYes"
},
render: function(){
$(this.el).html(_.template($('#promptPage-template').html(), this.model.toJSON()));
return this;
},
answerYes: function(){
alert('yes');
}
});
Run Code Online (Sandbox Code Playgroud)
我的规格是:
beforeEach(function() {
model = new PromptModel;
view = new PromptView({model:model});
loadFixtures('promptPage.tmpl');
});
it("should be able to answer a question with yes", function() {
var button = $("#btnYes", view.render().el);
expect(button.length).toBe(1);
spyOn(view, 'answerYes');
button.click();
expect(view.answerYes).toHaveBeenCalled();
});
Run Code Online (Sandbox Code Playgroud)
但是上面的视图定义在原型proto上创建了answerYes方法,但是间谍在视图中的实际实例上创建了一个函数,所以我最终得到了一个view.answerYes(),这是间谍和视图.__ proto __.answerYes,这是我真正想要窥探的那个.
如何创建一个间谍,以便它覆盖视图定义的answerYes方法?
spy ×10
jasmine ×5
javascript ×3
mocking ×3
testing ×3
java ×2
mockito ×2
angularjs ×1
backbone.js ×1
email ×1
geolocation ×1
jasmine2.0 ×1
jestjs ×1
php ×1
reactjs ×1
spock ×1
stub ×1
tracking ×1
unit-testing ×1
view ×1