jQuery的
$(".theme-picker").click(function () {
$('head').append('<link rel="stylesheet" href="" type="text/css" media="screen" id="theme_switcher"/>');
});
Run Code Online (Sandbox Code Playgroud)
茉莉花
describe("style.switcher", function() {
beforeEach( function() {
jasmine.getFixtures().set(
'<head></head>' +
'<div class="switcher">' +
'<a class="theme-picker" title="theme-picker"></a>' +
'<a class="folder-picker" title="folder-picker"></a>' +
'<a class="font-picker" title="font-picker"></a>' +
'<a class="color-picker" title="color-picker"></a>' +
'</div>' );
});
it("loads themes switcher link in head", function() {
$('.theme-picker').trigger('click');
expect( $('head') ).toContain("theme_switcher");
});
});
Run Code Online (Sandbox Code Playgroud)
我是茉莉花的新手,目前测试失败了.我不确定它是固定装置,触发器事件还是完全不同的东西.
我正在尝试使用Jasmine(带有yaml配置的gem)来测试Backbone.js应用程序.我正在使用像Todo示例那样的下划线模板.
template: _.template($('#item-template').html())
Run Code Online (Sandbox Code Playgroud)
我的问题是我无法在模型/视图之前加载模板,因此模板调用会导致这些类在加载时出错.
我已经阅读了关于jasmine-jquery插件来做夹具但问题是我的src文件(模型/视图)正在加载并且在我获得spec文件之前失败并且能够设置所需的灯具.
如何尽早加载模板,它们可用于重置我的类?
我正在使用jQuery-Jasmine扩展来监视事件,但我无法获得正确的语法.
// Get a button
var $button = $( '#ai1ec_subscribe_users' );
// Call the function
utility_functions.block_all_submit_and_ajax( $button.get(0) );
// check that all submit are disabled
var first_multi = $( '.ai1ec-facebook-refresh-multiselect:first' );
spyOnEvent( '.ai1ec-facebook-refresh-multiselect:first', 'click' );
first_multi.click();
expect( 'click' ).toHaveBeenTriggeredOn( '.ai1ec-facebook-refresh-multiselect:first' );
Run Code Online (Sandbox Code Playgroud)
这让我回来了
预期事件点击已在.ai1ec-facebook-refresh-multiselect上触发:首先
但我在检查前点击了它,所以我一定做错了.
当我运行我的茉莉花规格时,我收到以下错误:
Error: Expected a spy, but got undefined.
Run Code Online (Sandbox Code Playgroud)
我的coffeescript代码:
describe "setupForm", ->
beforeEach ->
spyOn(Subscription.prototype, 'runSimulation')
it "calls subscription.runSimulation when form is submitted with number", ->
Subscription.prototype.runSimulation()
expect(Subscription.prototype.runSimulation()).toHaveBeenCalled()
Run Code Online (Sandbox Code Playgroud)
我已经将我的错误代码简化为上面的调试,但是我无法弄清楚为什么当我明确地将它称为我的测试时它是从未调用过的.我正在其他地方测试该方法,所以我认为错误必须与我如何使用Jasmine Spy有关.谢谢.
每当我loadFixture()打电话或在本文下面进行测试时,我都会收到错误消息:
Frontend should be able to set fixtures.
TypeError: undefined is not a function
TypeError: undefined is not a function
at jasmine.Fixtures.cleanUp (http://localhost:4567/__spec__/helpers/jasmine-jquery.js:89:5)
at null.<anonymous> (http://localhost:4567/__spec__/helpers/jasmine-jquery.js:641:27)
at jasmine.Block.execute (http://localhost:4567/__jasmine__/jasmine.js:1064:17)
at jasmine.Queue.next_ (http://localhost:4567/__jasmine__/jasmine.js:2096:31)
at http://localhost:4567/__jasmine__/jasmine.js:2086:18
it("should be able to set fixtures", function() {
expect(setFixtures).toBeDefined(); // Notice I took out the ()
});
Run Code Online (Sandbox Code Playgroud)
似乎加载了jasmine-jquery,否则我得到ReferenceError: setFixtures is not defined错误.
关于发生了什么的任何线索?
我正在使用Jasmine 2.0测试Backbone View.View在加载时生成初始ajax请求,以使用值填充其表单字段.我希望我的Jasmine测试(在it下面的函数中)仅在收到这些值后运行.
测试当前失败,因为在测试运行后似乎收到了ajax请求,即使我正在存根ajax请求.
我该怎么写这个测试?
describe('View', function() {
var formFieldValues = [/* blah */];
var view;
beforeEach(function(done) {
jasmine.Ajax.install();
jasmine.Ajax.stubRequest('/form-fields').andReturn({
responseJSON: formFieldValues
});
view = new View({el: $('<main></main>')}); // makes ajax call on initialization
$('body').append(view.el);
});
afterEach(function() {
view.remove();
jasmine.Ajax.uninstall();
});
it('form fields have values', function(done) {
// PROBLEM IS HERE: THE FOLLOWING RUNS BEFORE VIEW'S AJAX CALL FINISHES
expect(view.$('[name="fieldName"]').children().length).toEqual(formFieldValues.length);
});
});
Run Code Online (Sandbox Code Playgroud) javascript backbone.js jasmine jasmine-jquery backbone-views
正在学习 Jasmine,想知道以下测试是否有效?如果没有,有人可以解释为什么吗?我已经阅读了许多教程,但找不到一个好的解释来帮助我理解为什么我似乎无法正确编写如下所示的测试。
// spec
describe("when cart is clicked", function() {
it("should call the populateNotes function", function() {
$("#show-cart").click()
expect(populateNotes()).toHaveBeenCalled();
})
})
// code
$("#show-cart").click(function() {
populateNotes();
})
Run Code Online (Sandbox Code Playgroud) 这是我的测试代码:
describe("Login", function(){
beforeEach(function(){
loadFixtures('login-fixture.html');
})
it("should enable the button when checking 'remember password'", function(){
$('#remember').trigger('click');
expect($('#keepIn')).not.toBeDisabled();
});
});
Run Code Online (Sandbox Code Playgroud)
这是我的生产代码:
$(document).ready(function(){
$('#remember').click(function(e) {
if($('#remember').is(':checked'))
{
$('#keepIn').removeAttr('disabled');
}
});
});
Run Code Online (Sandbox Code Playgroud)
这不起作用,生产代码永远不会被调用.我已经在触发事件之前和之后发出警报,并且在触发之后选中了复选框,但是没有调用.click函数.
有关为什么会发生这种情况的任何想法?
我是javascript测试新手。我正在使用茉莉花,需要测试是否已将正确的参数传递给方法。
这是我的方法:
function myView(){
if($('.view').is('.list')){
myWindow('list');
}else{
myWindow('random');
}
$('.view').toggleClass('my-list');
}
function myWindow(list) {
var url = /test.json;
$.post(url, {"list": list});
}
Here are my tests:
describe('#myView', function() {
beforeEach(function() {
fixture.load('myview.html');
});
it('sets window to list', function(){
expect(window.myWindow).toHaveBeenCalledWith('list');
});
});
Run Code Online (Sandbox Code Playgroud)
我收到以下错误。
Error: Expected a spy, but got Function.
Run Code Online (Sandbox Code Playgroud)
如果我在期望值之前添加此行(这似乎是错误的,因为我指定了应通过测试识别的正确参数)
spyOn(window, myWindow('list'));
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
undefined() method does not exist
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我写上述测试的好方法吗?
我很难理解茉莉花的spyOn功能.我写了一个简单的函数并测试我的方法是否被调用:
function myView() {
myLinks();
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试:
describe('#myView', function() {
it('updates link', function() {
var spyEvent = spyOn(window, 'myLinks');
expect(spyEvent).toHaveBeenCalled();
});
});
Run Code Online (Sandbox Code Playgroud)
这会返回以下故障:
Expected spy myLinks to have been called
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
jasmine ×10
jasmine-jquery ×10
javascript ×7
jquery ×6
backbone.js ×2
bdd ×1
coffeescript ×1
middleman ×1
templates ×1
unit-testing ×1