在AngularJS单元测试上设置$location.search

在AngularJS单元测试上设置$location.search,angularjs,unit-testing,phantomjs,karma-runner,Angularjs,Unit Testing,Phantomjs,Karma Runner,由于某种原因,当我试图用 $location.search('id', 2); 。。。在AngularJS单元测试(Karma+PhantomJS)中,即使设置了$apply,它也不会设置。如果我马上测试的话 expect($location.search().id).toEqual(2); 。。。它会告诉我ID是未定义的 我做错什么了吗?试试看 spyOn($location, 'search').and.returnValue({ id:2 }); 虽然此代码

由于某种原因,当我试图用

$location.search('id', 2);
。。。在AngularJS单元测试(Karma+PhantomJS)中,即使设置了$apply,它也不会设置。如果我马上测试的话

expect($location.search().id).toEqual(2);
。。。它会告诉我ID是未定义的

我做错什么了吗?

试试看

 spyOn($location, 'search').and.returnValue({
        id:2
    });

虽然此代码可能会回答问题,但可能会重复,提供有关此代码回答问题的原因和/或方式的附加上下文可提高其长期价值。