https://github.com/pivotal/jasmine/wiki/Matchers上的文档包括以下内容:
expect(function(){fn();}).toThrow(e);
Run Code Online (Sandbox Code Playgroud)
正如在讨论这个问题,下面就无法工作,因为我们想要一个函数对象传递给expect,而不是调用的结果fn()
expect(fn()).toThrow(e);
Run Code Online (Sandbox Code Playgroud)
问题1:以下工作如何?
expect(fn).toThrow(e);
Run Code Online (Sandbox Code Playgroud)
问题2:如果我thing用方法定义了一个对象,doIt下面的工作是否正常?
expect(thing.doIt).toThrow(e);
Run Code Online (Sandbox Code Playgroud)
(2a:如果是这样,有没有办法将参数传递给doIt方法?)
根据经验,答案似乎是肯定的,但我不相信我对js范围的理解足以确定.
谢谢!
我有以下功能可行
function sum ()
{
var total = 0,
num = 0,
numArgs = arguments.length;
if (numArgs === 0) {
throw new Error("Arguments Expected");
}
for(var c = 0; c < numArgs; c += 1) {
num = arguments[c];
if (typeof(num) !== "number") {
throw new Error("Only number are allowed but found", typeof (num));
}
total += num;
}
return total;
}
sum(2, "str"); // Error: Only number are allowed but found "string"
Run Code Online (Sandbox Code Playgroud)
茉莉花规格文件如下:
describe("First test; example specification", function () …Run Code Online (Sandbox Code Playgroud) 我是 Angular 应用程序单元测试的新手,我正在尝试测试我的第一个组件。实际上,我正在尝试测试实际组件使用的抽象基类,因此我在我的规范中基于它创建了一个简单的组件,并使用它来测试它。但是有一个处理 ( Injector)依赖项,我没有正确地将它存根,因为当我尝试运行测试时,我收到此错误:
Can't resolve all parameters for TestFormInputComponentBase
但我不确定我错过了什么?这是规范:
import { GenFormInputComponentBase } from './gen-form-input-component-base';
import { Injector, Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';
// We cannot test an abstract class directly so we test a simple derived component
@Component({
selector: 'test-form-input-component-base'
})
class TestFormInputComponentBase extends GenFormInputComponentBase {}
let injectorStub: Partial<Injector>;
describe('GenFormInputComponentBase', () => {
let baseClass: TestFormInputComponentBase;
let stub: Injector;
beforeEach(() => {
// stub Injector for test purpose
injectorStub = …Run Code Online (Sandbox Code Playgroud) 我有以下功能:
function checkforLandP(n){
if(n[0]=== '£' && n[n.length-1] === 'p'){
// remove the p
n =n.slice(0, -1);
// remove the £
n = n.substr(1);
// take the value and multiply by 100
n =Math.round(n*100);
if(isNaN(n)){
window.alert('Please enter a valid number');
throw ('Please enter a valid number');
}
}
return n;
};
Run Code Online (Sandbox Code Playgroud)
以及以下测试:
describe("check for £ and p", function(){
it("checks the array for £ and p together", function(){
expect(checkforLandP('£234p')).toBe(23400);
expect(checkforLandP(234)).toBe(234);
expect(checkforLandP('asdfsdf')).toBe('asdfsdf');
expect(checkforLandP('£asdfsdfp')).toThrow("Please enter a valid number");
});
});
Run Code Online (Sandbox Code Playgroud)
测试失败并返回:请输入抛出的有效数字
我一直在玩语法,几乎一切似乎都没问题.