96SEO 2026-02-19 06:56 9
最近的项目在使用AngulaJs,对JS代码的测试问题就摆在了面前。

通过对比我们选择了
https://github.com/jasmine/jasmine/releases
开始于调用全局Jasmine函数describe有两个参数一个字符串和一个函数。
expect(message).toMatch(/bar/);
expect(message).not.toMatch(/quux/);
expect(a.bar).not.toBeDefined();
expect(a.foo).not.toBeUndefined();
expect(pi).not.toBeLessThan(e);
expect(e).not.toBeGreaterThan(pi);
expect(foo).toThrowError(/bar/);
expect(foo).toThrowError(TypeError);
expect(foo).toThrowError(TypeError,
string参数用于命名specs的集合并且将与specs连接以构成spec的全名。
beforeEachafterEachbeforeAll和afterAll函数
//顾名思义beforeEach函数在调用它的describe中的每个
之前调用一次afterEach函数在每个spec之后调用一次。
//beforeAll函数仅在describe中的所有specs(测试用例)运行之前调用一次并且afterAll函数在所有specs(测试用例)完成后调用。
由于它们不在specs(测试用例)之间重置很容易在specs(测试用例)之间意外泄漏状态
//另一种在beforeEachit和afterEach之间共享变量的方法是通过this关键字。
afterEach都将这个作为同一个空对象对于下一个spec的beforeEach
expect(this.bar).toBe(undefined);
//调用describe可以嵌套在任何级别定义specs(测试用例)。
//在执行specs(测试用例)之前Jasmine沿着树顺序执行每个beforeEach函数。
//在没有函数体的情况下声明的任何specs(测试用例)也将在结果中被标记为待处理
如果你在specs(测试用例)体中任何地方调用该函数无论预期如何specs(测试用例)将被标记为待定。
//pending()函数接受一个字符串参数该参数会在结果集中显示在
只存在于描述或其定义的块中并且将在每个specs(测试用例)之后删除。
expect(foo.setBar).toHaveBeenCalled();
被调用了指定的次数toHaveBeenCalledTimes匹配器将通过。
expect(foo.setBar).toHaveBeenCalledTimes(2);
toHaveBeenCalledWith匹配器将返回true。
expect(foo.setBar).toHaveBeenCalledWith(123);
expect(foo.setBar).toHaveBeenCalledWith(456,
expect(foo.getBar).toHaveBeenCalled();
expect(fetchedBar).toEqual(123);
expect(foo.getBar).toHaveBeenCalled();
expect(fetchedBar).toEqual(745);
//直到它到达返回值列表的结尾此时它将返回未定义的所有后续调用。
getBar).and.returnValues(fetched
expect(foo.getBar).toHaveBeenCalled();
expect(foo.getBar()).toEqual(fetched
expect(foo.getBar()).toEqual(fetched
expect(foo.getBar()).toBeUndefined();
getBar).and.callFake(function(arguments,
expect(foo.getBar).toHaveBeenCalled();
expect(fetchedBar).toEqual(1001);
//.calls.any如果spy没有被调用则返回false如果至少有一个调用发生则返回true
expect(foo.setBar.calls.any()).toEqual(false);
expect(foo.setBar.calls.any()).toEqual(true);
expect(foo.setBar.calls.count()).toEqual(0);
expect(foo.setBar.calls.count()).toEqual(2);
//.calls.argsForindex返回传递给调用号索引的参数
expect(foo.setBar.calls.argsFor(0)).toEqual([123]);
expect(foo.setBar.calls.argsFor(1)).toEqual([456,
expect(foo.setBar.calls.allArgs()).toEqual([[123],[456,
//.calls.all返回上下文this和传递所有调用的参数
expect(foo.setBar.calls.all()).toEqual([{object:
//.calls.mostRecent返回上一次调用的上下文this和参数
expect(foo.setBar.calls.mostRecent()).toEqual({object:
//.calls.first返回上下文this和第一次调用的参数
expect(foo.setBar.calls.first()).toEqual({object:
//当检查来自allmostRecent和first的返回时当调用
expect(spy.calls.first().object).toBe(baz);
expect(spy.calls.mostRecent().object).toBe(quux);
expect(foo.setBar.calls.any()).toBe(true);
expect(foo.setBar.calls.any()).toBe(false);
//当没有一个函数来监视jasmine.createSpy可以创建一个“裸”
expect(whatAmI.and.identity()).toEqual(whatAmI);
expect(whatAmI).toHaveBeenCalled();
expect(whatAmI.calls.count()).toEqual(1);
expect(whatAmI).toHaveBeenCalledWith(I,
expect(whatAmI.calls.mostRecent().args[0]).toEqual(I);
的模拟使用jasmine.createSpyObj并传递一个字符串数组。
expect(tape.play).toBeDefined();
expect(tape.pause).toBeDefined();
expect(tape.stop).toBeDefined();
expect(tape.rewind).toBeDefined();
expect(tape.play).toHaveBeenCalled();
expect(tape.pause).toHaveBeenCalled();
expect(tape.rewind).toHaveBeenCalled();
expect(tape.stop).not.toHaveBeenCalled();
expect(tape.rewind).toHaveBeenCalledWith(0);
//如果实际值不为null或未定义jasmine.anything返回true。
expect(1).toEqual(jasmine.anything());
expect(foo).toHaveBeenCalledWith(12,
//与jasmine.objectContaining的部分匹配
//jasmine.objectContaining是用于期望在实际中只关心某些键/值对的时候。
describe(jasmine.objectContaining,
expect(foo).toEqual(jasmine.objectContaining({
expect(foo).not.toEqual(jasmine.objectContaining({
expect(callback).toHaveBeenCalledWith(jasmine.objectContaining({
expect(callback).not.toHaveBeenCalledWith(jasmine.objectContaining({
//部分数组与jasmine.arrayContaining相匹配
//jasmine.arrayContaining用于那些期望只关心数组中的某些值的时候。
describe(jasmine.arrayContaining,
expect(foo).toEqual(jasmine.arrayContaining([3,
expect(foo).not.toEqual(jasmine.arrayContaining([6]));
expect(callback).toHaveBeenCalledWith(jasmine.arrayContaining([4,
expect(callback).not.toHaveBeenCalledWith(jasmine.arrayContaining([5,
//jasmine.stringMatching用于当你不想完全匹配较大对象中的字符串时或者匹配
describe(jasmine.stringMatching,
jasmine.stringMatching(/^bar$/)});
expect(callback).toHaveBeenCalledWith(jasmine.stringMatching(bar));
expect(callback).not.toHaveBeenCalledWith(jasmine.stringMatching(/^bar$/));
//当您需要检查某个满足特定标准的条件而不是严格相等时您还可以通过提供具有asymmetricMatch函数的对象来指定自定义非对称等式测试器。
expect(foo,bar,baz,quux).toEqual(tester);
expect(callback).toHaveBeenCalledWith(tester);
jasmine.clock。
安装在需要操纵时间的spec或suite。
jasmine.createSpy(timerCallback);
//您可以使setTimeout或setInterval同步执行已注册的函数只有当时钟在时间上向前跳过时。
//要执行注册的函数通过jasmine.clock。
tick函数延时时间该函数
expect(timerCallback).not.toHaveBeenCalled();
expect(timerCallback).toHaveBeenCalled();
expect(timerCallback).not.toHaveBeenCalled();
expect(timerCallback.calls.count()).toEqual(1);
expect(timerCallback.calls.count()).toEqual(1);
expect(timerCallback.calls.count()).toEqual(2);
//如果你没有为mockDate提供基准时间它将使用当前日期。
jasmine.clock().mockDate(baseTime);
Date().getTime()).toEqual(baseTime.getTime()
Jasmine还支持运行需要测试异步操作的specs(测试用例)。
//调用beforeAllafterAllbeforeEachafterEach和它可以接受一个可选的单个参数当异步工作完成时应该调用。
//在done函数在调用之前这个specs(测试用例)不会开始。
expect(value).toBeGreaterThan(0);
expect(value).toBe(1);//所以这里value
//默认情况下jasmine将等待5秒钟异步specs(测试用例)在导致超时失败之前完成。
//如果超时在调用done之前超时则当前specs(测试用例)将被标记为失败并且单元测试执行将继续如同调用完成。
//如果特定规则应该更快失败或需要更多时间可以通过向其传递超时值等来调整。
//如果整个单元测试应该有不同的超时则可以在任何给定描述之外全局设置jasmine.DEFAULT_TIMEOUT_INTERVAL。
//done.fail函数使specs(测试用例)失败并指示它已完成
//这个工厂被传递给Jasmine理想的情况是调用beforeEach并且在一个给定的调用中描述的所有规范的范围内。
//自定义匹配器工厂传递两个参数util它有一组用于匹配器使用的效用函数见matchersUtil.js用于当前列表和customEqualityTesters
//工厂方法应该返回一个含有比较函数的对象该函数将被调用以检查期望值。
//toBeGoofy接受一个可选的期望参数所以如果不传递在这里定义。
jasmine.addMatchers(customMatchers);
作为专业的SEO优化服务提供商,我们致力于通过科学、系统的搜索引擎优化策略,帮助企业在百度、Google等搜索引擎中获得更高的排名和流量。我们的服务涵盖网站结构优化、内容优化、技术SEO和链接建设等多个维度。
| 服务项目 | 基础套餐 | 标准套餐 | 高级定制 |
|---|---|---|---|
| 关键词优化数量 | 10-20个核心词 | 30-50个核心词+长尾词 | 80-150个全方位覆盖 |
| 内容优化 | 基础页面优化 | 全站内容优化+每月5篇原创 | 个性化内容策略+每月15篇原创 |
| 技术SEO | 基本技术检查 | 全面技术优化+移动适配 | 深度技术重构+性能优化 |
| 外链建设 | 每月5-10条 | 每月20-30条高质量外链 | 每月50+条多渠道外链 |
| 数据报告 | 月度基础报告 | 双周详细报告+分析 | 每周深度报告+策略调整 |
| 效果保障 | 3-6个月见效 | 2-4个月见效 | 1-3个月快速见效 |
我们的SEO优化服务遵循科学严谨的流程,确保每一步都基于数据分析和行业最佳实践:
全面检测网站技术问题、内容质量、竞争对手情况,制定个性化优化方案。
基于用户搜索意图和商业目标,制定全面的关键词矩阵和布局策略。
解决网站技术问题,优化网站结构,提升页面速度和移动端体验。
创作高质量原创内容,优化现有页面,建立内容更新机制。
获取高质量外部链接,建立品牌在线影响力,提升网站权威度。
持续监控排名、流量和转化数据,根据效果调整优化策略。
基于我们服务的客户数据统计,平均优化效果如下:
我们坚信,真正的SEO优化不仅仅是追求排名,而是通过提供优质内容、优化用户体验、建立网站权威,最终实现可持续的业务增长。我们的目标是与客户建立长期合作关系,共同成长。
Demand feedback