Пример #1
0
        /// <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);
        }
Пример #2
0
        /// <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);
        }
Пример #3
0
        /// <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);
        }
Пример #4
0
        /// <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);
        }
Пример #5
0
        /// <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));
        }
Пример #6
0
        /// <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));
        }
Пример #7
0
        /// <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)));
        }
Пример #8
0
        /// <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()));
        }