对于不鼓励在同一字段上使用@Spy和@InjectMocks吗?感兴趣的读者,本文将提供您所需要的所有信息,并且为您提供关于$inject.instantiateVS$inject.getVS$inj
对于不鼓励在同一字段上使用@Spy和@InjectMocks吗?感兴趣的读者,本文将提供您所需要的所有信息,并且为您提供关于$inject.instantiate VS $inject.get VS $injector.invoke in angularjs、@Mock和@InjectMocks的多个级别、ASP.NET MVC和JQuery同时在同一字段上显示多个验证错误、com.google.inject.internal.InjectorImpl.InjectorOptions的实例源码的宝贵知识。
本文目录一览:- 不鼓励在同一字段上使用@Spy和@InjectMocks吗?
- $inject.instantiate VS $inject.get VS $injector.invoke in angularjs
- @Mock和@InjectMocks的多个级别
- ASP.NET MVC和JQuery同时在同一字段上显示多个验证错误
- com.google.inject.internal.InjectorImpl.InjectorOptions的实例源码
不鼓励在同一字段上使用@Spy和@InjectMocks吗?
在我目前正在处理的项目中,我经常看到@Spy
和@InjectMocks
在字段中一起使用。我从未在任何教程或其他资源中以这种方式看到过它。我用谷歌搜索了这个特定的组合,但是除了在GitHub上的这个线程外没有找到其他东西:https
:
//github.com/mockito/mockito/issues/169
这让我觉得我们在以一种奇怪的方式使用它。
注意:我认为有时同时使用两个批注是有道理的,因为如果仅使用@InjectMocks
Mockito,则尝试使用无参数构造函数实例化该类。如果没有no-
args构造@Spy
函数,则可以添加该对象而无需使用空构造函数。
编辑:另一个重要的用途是,如果仅使用两个注释,则只能对方法进行存根。
答案1
小编典典一起使用@Spy和@InjectMocks是不常见的,并且可以说是不合适的。
@InjectMocks
充当被测系统的一种依赖项注入:如果您有定义正确类型的@Mock或@Spy的测试,则Mockito将使用这些字段初始化@InjectMocks实例中的任何字段。如果您没有以其他方式构造要测试的系统以进行依赖项注入(或者如果您使用进行字段注入的DI框架)并且想用模拟替换那些依赖项,那么这可能会很方便。它可能非常脆弱-
不匹配的字段将被静默忽略,并且null
如果未在初始化程序中设置,这些字段将保留下来-但是 对于测试中的系统 仍然是一个不错的注释。
@Spy 和@Mock一样,旨在设置 测试双打 ;当您有想要存根或验证的协作者时,应使用它。请注意,@Spy和@Mock始终
用于依赖项,而不用于受测系统 。
理想情况下,您不应拥有在同一测试中同时担当这两个角色的任何类,否则您可能会发现自己编写了一个艰苦的测试,而不是实际的生产行为。在任何情况下,要准确说明测试涵盖的内容与您的举止行为都将更加困难。
当然,如果您尝试使用Mockito单独测试单个方法,并且想在测试另一个方法时对一个方法进行调用,则此方法可能不适用。但是,这也可能表明您的班级违反了“单一责任原则”,您应该将班级分解为可以协同工作的多个独立班级。然后,在测试中,您可以允许实例只具有一个角色,而无需一次使用两个注释。
$inject.instantiate VS $inject.get VS $injector.invoke in angularjs
app.service('myService',function ($q,$http) { return { q: $q,http: $http }; });
$injector.get(name,[caller]);
返回所请求服务的实例.
$injector.get('myService'); // { q: $q,http: $http }
$injector.invoke(fn,[self],[localals]);
调用提供的方法,并从$inject中传递给定的参数.
$injector.invoke(function (myService,$http) { console.log(myService); // { q: $q,http: $http }; console.log(this); // { v: 'im this!' }; console.log($http); // null },{ v: 'im this!' },{ $http: null });
$injector.instantiate(Type,[locals]);
创建给定类型的新实例.使用构造函数,然后使用构造函数注释中指定的参数调用新实例.
假设以下’class’:
function Person (fName,lName,$http,$q) { return { first_name: fName,last_name: lName,http: $http,q: $q } }
现在,如果我们想在我们的控制器中创建一个新的Person,我们可以这样做:
app.controller('...',function ($injector) { var $http = $injector.get('$http'); var $q = $injector.get('$q'); var p = new Person('kasper','lewau',$q); console.log(p); // { first_name: 'kasper',last_name: 'lewau',q: $q }; });
想象一下,人有〜20个依赖关系,我们用$inject.get方法获取每个人的每一个.
繁琐!而且 – 你需要保持你的参数&参数同步啊.
相反,你可以这样做:
app.controller('...',function ($injector) { var p = $injector.instantiate(Person,{ fName: 'kasper',lName: 'lewau' }); console.log(p); // { first_name: 'kasper',q: $q }; });
而且 – 如果我们想要,我们可以提供本地的.instantiate调用,以便覆盖内部$inject.get()在实例化时通常会得到的内容.
var p = $injector.instantiate(Person,{ fName: 'kasper',lName: 'lewau' },{ $http: 'nothing!',$q: 'nothing!' }); console.log(p); // { first_name: 'kasper',http: 'nothing!',q: 'nothing!' };
我希望解释三者之间的区别.如果您需要更多有关差异的信息,我会推荐这些文章:
> http://taoofcode.net/studying-the-angular-injector/
> http://taoofcode.net/studying-the-angular-injector-annotate/
> http://taoofcode.net/studying-the-angular-injector-invoke/
> http://taoofcode.net/studying-the-angular-injector-getservice/
> http://taoofcode.net/studying-the-angular-js-injector-instantiate/
@Mock和@InjectMocks的多个级别
因此,我了解到在Mockito中,@ InjectMocks将使用@Mock的注解注入所有可以注入的内容,但是如何处理这种情况呢?
@Mock
private MockObject1 mockObject1;
@Mock
private MockObject2 mockObject2;
@InjectMocks
private SystemUnderTest systemUnderTest = new SystemUnderTest();
假设MockObject2的属性类型为MockObject1,而SystemUnderTest的属性类型为MockObject2。我想将模拟对象1注入到模拟对象2中,并将模拟对象2注入到systemUnderTest中。
注释可以吗?
ASP.NET MVC和JQuery同时在同一字段上显示多个验证错误
如何解决ASP.NET MVC和JQuery同时在同一字段上显示多个验证错误?
我有一张注册表,并实现了一些我想在客户端进行验证的密码验证属性。
这是验证属性之一的代码:
public sealed class ContainsUpperAndLowerCaseAttribute : RegularExpressionAttribute,IClientValidatable
{
private const string _pattern = @"^(?=.*[a-z])(?=.*[A-Z])[A-Za-z\d$@$!%*?&]*";
public ContainsUpperAndLowerCaseAttribute() : base(_pattern)
{
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata Metadata,ControllerContext context)
{
var rule = new ModelClientValidationRule()
{
ValidationType = "upperlower",ErrorMessage = ErrorMessage
};
rule.ValidationParameters.Add("pattern",_pattern);
yield return rule;
}
}
我有两个具有相同正则表达式和ValidationType
属性的相同属性。
视图模型的代码为:
public class Registerviewmodel
{
[required(ErrorMessage = "This field is required")]
[EmailAddress]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[DataType(DataType.Password)]
[required(ErrorMessage = "This field is required")]
[StringLength(100,ErrorMessage = "Should be at least 8 characters",MinimumLength = 8)]
[ContainsDigits(ErrorMessage = "Password should contain digits")]
[ContainsUpperAndLowerCase(ErrorMessage = "Password should contain upper and lower case characters"))]
[ContainsNonLeterandDigitSymbol(ErrorMessage = "Password should contain non letter and non digit characters")]
public string Password { get; set; }
}
然后我将这些规则应用于JS中的表单:
$.validator.addMethod("upperlower",function (value,element,regexp) {
var re = new RegExp(regexp); return re.test(value);
});
$.validator.addMethod("digits",regexp) {
var re = new RegExp(regexp); return re.test(value);
});
$.validator.addMethod("symbols",regexp) {
var re = new RegExp(regexp); return re.test(value);
});
jQuery.validator.unobtrusive.adapters.addSingleVal("digits","pattern");
jQuery.validator.unobtrusive.adapters.addSingleVal(''upperlower'',"pattern");
jQuery.validator.unobtrusive.adapters.addSingleVal(''symbols'',"pattern");
但是,当我测试此代码时,UI中仅显示一个验证错误,并在控制台中使用以下命令进行检查:console.log($(''#form'').validate().errorList)
-它仅包含一个错误。我确信所有验证属性都可以正常工作,因为它们显示为一个一个,如:如果我键入不带数字的密码,则表明我应该添加数字,然后表明我应该添加大写字符,但我想显示所有错误。同时。
如果可以的话,请给我建议实施这样的事情?
谢谢您的帮助
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
com.google.inject.internal.InjectorImpl.InjectorOptions的实例源码
public Object createProxy(InjectorOptions injectorOptions,Class<?> expectedType) throws InternalProvisionException { if (injectorOptions.disableCircularProxies) { throw InternalProvisionException.circularDependenciesdisabled(expectedType); } if (!expectedType.isInterface()) { throw InternalProvisionException.cannotProxyClass(expectedType); } if (invocationHandlers == null) { invocationHandlers = new ArrayList<>(); } DelegatingInvocationHandler<T> invocationHandler = new DelegatingInvocationHandler<>(); invocationHandlers.add(invocationHandler); // Todo: if I create a proxy which implements all the interfaces of // the implementation type,I'll be able to get away with one proxy // instance (as opposed to one per caller). ClassLoader classLoader = BytecodeGen.getClassLoader(expectedType); return expectedType.cast( Proxy.newProxyInstance( classLoader,new Class[] {expectedType,CircularDependencyProxy.class},invocationHandler)); }
InjectorOptions getoptions(Stage stage,InjectorOptions parentOptions) { checkNotNull(stage,"stage must be set"); if (parentOptions == null) { return new InjectorOptions( stage,jitdisabled,disableCircularProxies,atInjectrequired,exactBindingAnnotationsrequired); } else { checkState(stage == parentOptions.stage,"child & parent stage don't match"); return new InjectorOptions( stage,jitdisabled || parentOptions.jitdisabled,disableCircularProxies || parentOptions.disableCircularProxies,atInjectrequired || parentOptions.atInjectrequired,exactBindingAnnotationsrequired || parentOptions.exactBindingAnnotationsrequired); } }
InjectorOptions getoptions(Stage stage,"stage must be set"); if(parentOptions == null) { return new InjectorOptions( stage,exactBindingAnnotationsrequired || parentOptions.exactBindingAnnotationsrequired); } }
InjectorOptions getoptions(Stage stage,exactBindingAnnotationsrequired || parentOptions.exactBindingAnnotationsrequired); } }
InternalContext(InjectorOptions options,Object[] toClear) { this.options = options; this.toClear = toClear; this.enterCount = 1; }
InjectorOptions getInjectorOptions() { return options; }
今天的关于不鼓励在同一字段上使用@Spy和@InjectMocks吗?的分享已经结束,谢谢您的关注,如果想了解更多关于$inject.instantiate VS $inject.get VS $injector.invoke in angularjs、@Mock和@InjectMocks的多个级别、ASP.NET MVC和JQuery同时在同一字段上显示多个验证错误、com.google.inject.internal.InjectorImpl.InjectorOptions的实例源码的相关知识,请在本站进行查询。
本文标签: