I notice that there are two approaches, when setting controller's unit tests - scope = {} and scope = $rootScope.$new()
我注意到,在设置controller的单元测试时,有两种方法—scope ={}和scope = $rootScope。
What I need to know is the reasoning for choosing one over the other. Unfortunately, I haven't seen any explanation why one is used and the other is not.
我需要知道的是选择其中之一的原因。不幸的是,我没有看到任何解释为什么一个被使用而另一个没有被使用。
I suppose, that scope = {} will be faster, but is there any other advance?
我认为,scope ={}将会更快,但是还有其他改进吗?
1 个解决方案
#1
2
If you want to call methods on the scope such as .$watch
or anything else, they need to be defined on the scope
object that you are using. {}
(empty object) has no such methods, but $rootScope.$new
does.
如果您想要调用范围上的方法,例如.$watch或任何其他方法,则需要在您正在使用的范围对象上定义它们。{}(空对象)没有这样的方法,而是$rootScope。新的美元。
If you don't need to use such methods it doesn't actually matter what you use, and you could stub or define those methods on an object and inject that as the scope as well. It depends on what you need for your tests.
如果您不需要使用这些方法,那么实际上使用什么并不重要,您可以在对象上存根或定义这些方法,并将其作为范围注入。这取决于你的测试需要什么。
#1
2
If you want to call methods on the scope such as .$watch
or anything else, they need to be defined on the scope
object that you are using. {}
(empty object) has no such methods, but $rootScope.$new
does.
如果您想要调用范围上的方法,例如.$watch或任何其他方法,则需要在您正在使用的范围对象上定义它们。{}(空对象)没有这样的方法,而是$rootScope。新的美元。
If you don't need to use such methods it doesn't actually matter what you use, and you could stub or define those methods on an object and inject that as the scope as well. It depends on what you need for your tests.
如果您不需要使用这些方法,那么实际上使用什么并不重要,您可以在对象上存根或定义这些方法,并将其作为范围注入。这取决于你的测试需要什么。