【发布时间】:2014-03-10 06:10:25
【问题描述】:
我正在尝试使用 Karma 和 Jasmine 为使用 Dropbox Datastore api 的 javascript 应用程序创建一些测试。 这是使用来自https://www.dropbox.com/developers/datastore/tutorial/js的介绍性 Dropbox 代码的简化测试
在运行测试之前,我已经在浏览器中使用 Dropbox 手动授权了应用程序,但是当我运行测试时,它说客户端没有经过身份验证并且没有发生错误。在运行测试时是否需要做一些额外的事情才能对其进行身份验证?
'use strict';
describe('dropbox', function () {
var client = null;
beforeEach(function() {
client = new Dropbox.Client({key: '46tjf8x15q98xic'});
// Try to finish OAuth authorization.
client.authenticate({interactive: false}, function (error) {
if (error) {
alert('Authentication error: ' + error);
}
});
});
it('client is not null', function() {
expect( client ).not.toBeNull();
});
it('authenticated is true', function() {
expect( client.isAuthenticated() ).toEqual( true );
});
});
Running "karma:unit" (karma) task
INFO [karma]: Karma v0.10.9 server started at http://localhost:8080/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 33.0.1750 (Mac OS X 10.9.2)]: Connected on socket BKoS8rqqeeL7fg3cHEQl
Chrome 33.0.1750 (Mac OS X 10.9.2) dropbox authenticated is true FAILED
Expected false to equal true.
Error: Expected false to equal true.
at null.<anonymous> (/Users/davidsmith/Sites/myapp/test/spec/dropbox.js:23:38)
Chrome 33.0.1750 (Mac OS X 10.9.2): Executed 2 of 2 (1 FAILED) (0.301 secs / 0.009 secs)
Warning: Task "karma:unit" failed. Use --force to continue.
Aborted due to warnings.
【问题讨论】:
-
我对 Karma 和 Jasmine 一无所知,但我的猜测是在您在浏览器中手动进行身份验证和运行测试之间没有保留本地存储。尝试一个更简单的测试,例如在浏览器中设置 localStorage.foo = 'bar',然后测试您是否可以看到该值?
-
啊,好主意。我刚试了一下,运行测试时找不到我在浏览器中设置的“foo”。接下来,我尝试在测试期间设置并获取 localStorage 值,并且成功了。我想我接下来要尝试的是从浏览器中复制 dropbox 的本地存储键/值对,并在测试期间将其添加到本地存储中,看看是否可行。
标签: javascript jasmine dropbox dropbox-api karma-runner