我有一个AngularJS WebApp,我用Karma和Jasmine.js进行测试
但如果我在我的控制器中使用navigator.webkitGetUserMedia(),则会抛出错误。
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;
using Xamarin.Forms;
using AddressBookUI;
using AddressBook;
namespace TestContacts.iOS
{
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
UIWindow window;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init ();
window = new UIWindow(UIScreen.MainScreen.Bounds);
var nav =
new UINavigationController(new App ().MainPage.CreateViewController());
ContactsShared.Instance = new TouchContacts (nav);
window.RootViewController = nav;
window.MakeKeyAndVisible();
return true;
}
}
public class TouchContacts : IContactsShared {
UINavigationController nav;
public TouchContacts(UINavigationController nav){
this.nav = nav;
}
public void Show() {
var newPersonController = new ABNewPersonViewController();
newPersonController.NewPersonComplete +=
(object sender, ABNewPersonCompleteEventArgs e) =>
{
nav.PopViewController(true);
};
var person = new ABPerson();
person.FirstName = "John";
person.LastName = "Doe";
newPersonController.DisplayedPerson = person;
nav.PushViewController(newPersonController, true);
}
}
}
以下是我的控制器
的示例TypeError: 'undefined' is not a function (evaluating 'navigator.webkitGetUserMedia')
my testspec
app.controller('appCtrl', function ($scope, $interval, $location, globVal, loginService) {
navigator.webkitGetUserMedia({video: true, audio: false},
function (stream) {
globVal.webcam = window.URL.createObjectURL(stream);
},
function (err) {
console.log("error happened:" + err);
}
);
$scope.startTimeout = $interval(function () {
// do something
}, 2000);
});
describe(“Controller:appCtrl”,function(){ var $ rootScope, $范围, 控制器, globVal, 位置;
'use strict';
我应该如何模拟导航器对象?
答案 0 :(得分:0)
使用spyOn(navigator, 'webkitGetUserMedia');
如果这不起作用,只需在测试规范中创建自己的webkitGetUserMedia函数作为导航器对象的一部分。
答案 1 :(得分:0)
您可以在beforeEach或其中注入$ window(" should ...")函数,并在$ window中替换整个导航器对象。
像这样:
it("should ... when running in an obscure environment", inject(function ($window) {
$window.navigator = {
userAgent: 'Mozilla/5.0 (NodeJS; Karma 0.0.0) (Cucumber/0.0 like virtualisation) Fantasy/0.0',
appVersion: '5.0 (NodeJS; Karma 0.0.0) (Cucumber/0.0 like virtualisation) Fantasy/0.0',
platform: 'nodeKarma'
};
scope.$digest();
.......
}));
确保包含足够的属性!
答案 2 :(得分:0)
问题很可能是您在karma.conf.js中未使用协议“ https:”
在添加以下行(当然带有正确的证书)之前,我遇到了完全相同的问题。
config.set({
protocol: 'https:',
httpsServerOptions: {
key: fs.readFileSync('server.key', 'utf8'),
cert: fs.readFileSync('server.crt', 'utf8')
},