标签: jasmine-jquery

我如何在Jasmine中测试$ scope.watch(AngularJS)的变化?

我刚刚开始用AngularJS写一些东西,我不知道如何为这个特殊的东西编写测试.我正在建立一个具有不同状态的"帮助请求"模式.所以在我的控制器中,我使用$ scope.request_mode变量.激活帮助请求的不同链接将该变量设置为不同的变量.

然后在我的指令中,我正在做一个$scope.$watch('request_mode', function(){...});选择性激活或停用事物,因为请求模式发生了变化.代码都很好用,但我遇到的问题是测试.我似乎无法让Jasmine拿起$scope.$watch并且在它发生变化时实际开火.

我敢肯定有人之前遇到过这种情况,所以任何建议都会非常感激.

jquery jasmine jasmine-jquery angularjs angularjs-directive

36
推荐指数
1
解决办法
2万
查看次数

如何解决Jasmine单元测试中的$ q.all承诺?

我的控制器有如下代码:

$q.all([qService.getData($scope.id), dService.getData(), qTService.get()])
.then(function (allData) {
  $scope.data1 = allData[0];
  $scope.data2 = allData[1];
  $scope.data3 = allData[2];
});
Run Code Online (Sandbox Code Playgroud)

在我的单元测试中,我正在做这样的事情:

beforeEach(inject(function($rootScope, $q, $location){// and other dependencies... 
  qServiceSpy = spyOn(_qService, 'getData').andCallFake(function () {
    var data1 = {
      id: 1,
      sellingProperty: 1,
    };
    var d = $q.defer();
    d.resolve(data1);
    return d.promise;
  });

  dServiceSpy = spyOn(_dService, 'getData').andCallFake(function () {
    var data2 = [{ "id": "1", "anotherProp": 123 }];
    var d = $q.defer();
    d.resolve(data2);
    return d.promise;
  });
  qTServiceSpy = spyOn(_qTService, 'get').andCallFake(function () {
    var data3 = [{ …
Run Code Online (Sandbox Code Playgroud)

unit-testing jasmine jasmine-jquery angularjs karma-runner

31
推荐指数
1
解决办法
2万
查看次数

如何在不使用外部html文件的情况下在茉莉花测试中添加DOM元素?

我正在写一些简单的茉莉花测试,我得到一个例外,因为我正在测试的代码正在寻找一个不存在的表单,因为在测试js文件时没有DOM:$("form")[0]在测试的js文件中导致:

TypeError: $(...)[0] is undefined
Run Code Online (Sandbox Code Playgroud)

我读了一下jasmine-jquery,并意识到我可以使用一些外部html文件的html fixture.这个流程似乎非常混乱,因为我只需要添加一个空的有效表单,以便测试(专注于其他内容)将会运行,<form></form>我认为附加就足够了.

起初我认为sandbox()函数将是解决方案,但它似乎只创建div,我需要一个表单.

通过仅使用jasmine规范文件中的代码添加一些元素的任何简单方法?

jasmine jasmine-jquery

16
推荐指数
1
解决办法
2万
查看次数

使用jasmine点击事件调用如何断言间谍?

我正在写一个简单的点击处理程序,需要传入的事件(如此)

Thing = function($){

    var MyObject = function(opts){
        this.opts = opts;
    };

    MyObject.prototype.createSomething = function(){
        var that = this;
        $('#some_dom_element').live('click', function(e) {
            that.doStuff(e);
        });
    };

    MyObject.prototype.doStuff = function(e) {
        //do some javascript stuff ...
        e.preventDefault();
    };

    return MyObject;

}(jQuery);
Run Code Online (Sandbox Code Playgroud)

目前在我的jasmine规范中,我有一些东西可以监视我期望被调用的函数(但是因为它被调用e -not而没有args-我的断言失败了)

    it ("live click handler added to the dom element", function(){
        var doSpy = spyOn(sut, 'doStuff');
        sut.createSomething();
        $("#some_dom_element").trigger('click');
        expect(doSpy).toHaveBeenCalledWith();
    });
Run Code Online (Sandbox Code Playgroud)

我如何纠正这个"toHaveBeenCalledWith"按照我的预期工作?


UPDATE

我无法按原样获得接受的答案,但我能够稍微改变它,以下是我100%工作的例子

    it ("should prevent default on click", function(){
        var event = {
            type: 'click',
            preventDefault: function () {} …
Run Code Online (Sandbox Code Playgroud)

javascript dom-events jasmine jasmine-jquery

12
推荐指数
1
解决办法
2万
查看次数

无法获得$ .ajax.mostRecentCall与jasmine 2.0.2一起使用

找到一个简单的例子工作真的很麻烦.我使用的这个例子来自https://gist.github.com/Madhuka/7854709

describe("Test for spies", function() {
 function sendRequest(callbacks, configuration) {
        $.ajax({
            url: configuration.url,
            dataType: "json",
            success: function(data) {
                callbacks.checkForInformation(data);
            },
            error: function(data) {
                callbacks.displayErrorMessage();
            },
            timeout: configuration.remainingCallTime
        });
    }

    it("should make an Ajax request to the correct URL", function() {

    var configuration = {
        url : "http://www.google.com",
        remainingCallTime : 30000
    };

        spyOn($, "ajax");

        sendRequest(undefined, configuration);
        expect($.ajax.mostRecentCall.args[0]["url"]).toEqual(configuration.url);
    });
});
Run Code Online (Sandbox Code Playgroud)

无论出于何种原因,$.ajax.mostRecentCall都是未定义的.

使用jasmine 2.0.2和jasmine jquery 2.0.5.

在这里小提琴:http://jsfiddle.net/sidouglas/85b35993/

javascript ajax jquery jasmine jasmine-jquery

12
推荐指数
1
解决办法
2932
查看次数

Mocha兼容的灯具支持

我希望将Jasmine测试转换为Mocha测试,因为它能够执行Before(all),报告功能以及对coffeescript的支持.

我无法找到的一件事是Mocha(或与兼容的断言库一起)是否支持像jasmine-jquery那样的工具(https://github.com/velesin/jasmine-jquery).摩卡有这样的功能吗?

我正在尝试测试一些操作DOM的浏览器内代码(因此我将使用html测试运行程序运行它).我不想使用zombiejs,因为我更喜欢在我正在测试的环境中运行它.

编辑:仅供参考,在搜索解决方案时,我遇到了使用grunt + mocha和phantomjs实现.这对我的情况也不起作用,因为我想使用webkit的自定义版本.

testing bdd jquery mocha.js jasmine-jquery

10
推荐指数
1
解决办法
4841
查看次数

使用.toHaveBeenCalledWith的Jasmine测试失败,用于注册表单

我正在处理的单页面应用程序有一个登录视图,有两种形式:登录表单和注册表单.以下规范描述了这些表单的测试.我正在使用Jasmine-jQuery 1.4.2.

// user_spec.js

describe("User", function() {

  var userController;

  beforeEach(function () {
    loadFixtures('menu.html');
    userController = new MyApp.User.Controller();
  });

  describe("LoginView", function() {

    beforeEach(function() {
      // Mock the $.ajax function to prevent XHR:
      spyOn($, "ajax").andCallFake(function(params) {});
    });

    it("should pass email and password with the 'signInForm:submit' event.", function() {
      var email = "firstname.name@email.com";
      var password = "Passw0rd";
      var callback = jasmine.createSpy("FormSubmitSpy");

      userController.loginView.$el.find("#signInEmail").val(email);
      userController.loginView.$el.find("#signInPassword").val(password);
      userController.loginView.bind("signInForm:submit", callback, this);
      userController.loginView.ui.signInForm.trigger("submit");

      expect(callback).toHaveBeenCalledWith({
        email: email,
        password: password
      });
    });

    it("should pass name, email and password with the …
Run Code Online (Sandbox Code Playgroud)

javascript jquery-callback jasmine jasmine-jquery single-page-application

10
推荐指数
1
解决办法
1796
查看次数

茉莉花间谍没被召唤

我在茉莉花的间谍活动中遇到了一些麻烦

我想检查是否使用jasmine spy和jasmine jquery在滑块上单击了一个链接.

这是一个简化版本:

我有一些链接作为html fixture文件的一部分.

<a href="#" class="someLink">Link 1</a>
<a href="#" class="someLink">Link 2</a>
Run Code Online (Sandbox Code Playgroud)

滑块:

var Slider = function(links){
    this.sliderLinks = $(links);
    this.bindEvents();
}

Slider.prototype.bindEvents = function(){
    this.sliderLinks.on('click', this.handleClick);
}

Slider.prototype.handleClick = function(e){
    console.log('i have been clicked')
}
Run Code Online (Sandbox Code Playgroud)

规范文件:

describe('Slider', function(){
    var slider;

    beforeEach(function(){
        loadFixtures('slider.html');

        slider = new Slider('.someLink');

    });

    it('should handle link click', function(){
        spyOn(slider, 'handleClick');
        $(slider.sliderLinks[0]).trigger('click');
        expect(slider.handleClick).toHaveBeenCalled();
    });

});
Run Code Online (Sandbox Code Playgroud)

测试失败了.但是"我已被点击"已被记录到控制台,因此正在调用该方法.

如果我这样做,测试通过:

it('should handle link click', function(){
        spyon(slider, 'handleClick');
        slider.handleClick();
        expect(slider.handleClick).toHaveBeenCalled();
    });
Run Code Online (Sandbox Code Playgroud)

所以我的问题基本上是:

  1. 我是以错误的方式测试这种类型的东西吗?
  2. 为什么间谍没有注册这个方法被调用的事实?

jasmine jasmine-jquery

10
推荐指数
1
解决办法
1万
查看次数

在Visual Studio 2013中使用Jasmine + Karma进行AngularJS测试

我是Angularjs框架的新手,也是使用Jasmine框架和Karma测试它的.我有一个使用Visual Studio 2013构建的ASP.NET MVC应用程序,我想包含Jasmine包和Karma以测试js.

也许这个问题已经在这里讨论了,但是请您展示如何包含Jasmine文件的教程或演示,并测试并显示结果?对于测试spa应用程序是Jasmine和Karma的最佳选择吗?

你能否告诉我一些在项目中包含Jasmine的步骤以及如何将它与我绑定.js以便测试它?

jquery asp.net-mvc-4 jasmine-jquery karma-jasmine

9
推荐指数
1
解决办法
6313
查看次数

如何修复 Jasmine 中已发现的函数错误

我有 3 个测试,每个测试都测试不同的方法。

it('test function1', function() {
   spyOn(document, 'getElementById');
   // ... some code to test function1
   expect(document.getElementById).toHaveBeenCalled();

 });

it('test function2', function() {
   spyOn(document, 'getElementById');
   // ... some code to test function2
   expect(document.getElementById).toHaveBeenCalled();

 });

it('test function3', function() {
   spyOn(document, 'getElementById');
   // ... some code to test function3
   expect(document.getElementById).toHaveBeenCalled();    
 });
Run Code Online (Sandbox Code Playgroud)

但是当我运行这些测试时,我收到以下错误:getElementById has already been spied upon。有人可以解释为什么即使间谍位于不同的测试套件中我也会收到此错误以及如何修复它。

javascript jasmine jasmine-jquery karma-jasmine jasmine2.0

9
推荐指数
2
解决办法
3万
查看次数