/// <summary> /// Creates a rule for a property. /// </summary> public Faker <T> RuleFor <TProperty>(Expression <Func <T, TProperty> > property, Func <Faker, TProperty> setter) { var propName = PropertyName.For(property); Func <Faker, T, object> invoker = (f, t) => setter(f); this.Actions.Add(propName, invoker); return(this); }
/// <summary> /// Ignore a property or field when using StrictMode. /// </summary> /// <typeparam name="TPropertyOrField"></typeparam> /// <param name="propertyOrField"></param> /// <returns></returns> public Faker <T> Ignore <TPropertyOrField>(Expression <Func <T, TPropertyOrField> > propertyOrField) { var propNameOrField = PropertyName.For(propertyOrField); if (!this.TypeProperties.Remove(propNameOrField)) { throw new ArgumentException($"The property or field {propNameOrField} was not found on {typeof(T)} during the binding discovery of T. Can't ignore something that doesn't exist."); } return(this); }
/// <summary> /// Ignore a property or field when using StrictMode. /// </summary> /// <typeparam name="TPropertyOrField"></typeparam> /// <param name="propertyOrField"></param> /// <returns></returns> public Faker <T> Ignore <TPropertyOrField>(Expression <Func <T, TPropertyOrField> > propertyOrField) { var propNameOrField = PropertyName.For(propertyOrField); if (!this.TypeProperties.TryGetValue(propNameOrField, out MemberInfo mi)) { throw new ArgumentException($"The property or field {propNameOrField} was not found on {typeof(T)} during the binding discovery of T. Can't ignore something that doesn't exist."); } this.Ignores.Add(currentRuleSet, propNameOrField); return(this); }
/// <summary> /// Creates a rule for a compound property and providing access to the instance being generated. /// </summary> public Faker <T> RuleFor <TProperty>(Expression <Func <T, TProperty> > property, Func <Faker, T, TProperty> setter) { var propName = PropertyName.For(property); Func <Faker, T, object> invoker = (f, t) => setter(f, t); var rule = new PopulateAction <T> { Action = invoker, RuleSet = currentRuleSet, PropertyName = propName }; this.Actions.Add(currentRuleSet, propName, rule); return(this); }
/// <summary> /// Creates a rule for a property. /// </summary> public Faker <T> RuleFor <TProperty>(Expression <Func <T, TProperty> > property, Func <Faker, TProperty> setter) { var propName = PropertyName.For(property); return(RuleFor(propName, setter)); }
/// <summary> /// Ignores a property or field when <seealso cref="StrictMode"/> is enabled. /// </summary> public virtual Faker <T> Ignore <TPropertyOrField>(Expression <Func <T, TPropertyOrField> > propertyOrField) { var propNameOrField = PropertyName.For(propertyOrField); return(Ignore(propNameOrField)); }
/// <summary> /// Creates a rule for a property. /// </summary> public virtual Faker <T> RuleFor <TProperty>(Expression <Func <T, TProperty> > property, Func <Faker, TProperty> setter) { var propName = PropertyName.For(property); return(AddRule(propName, (f, t) => setter(f))); }
/// <summary> /// Creates a rule for a property. /// </summary> public virtual Faker <T> RuleFor <TProperty>(Expression <Func <T, TProperty> > property, Func <TProperty> valueFunction) { var propName = PropertyName.For(property); return(AddRule(propName, (f, t) => valueFunction())); }