AutoFake icon indicating copy to clipboard operation
AutoFake copied to clipboard

Provide MemberExpression example (MemberInfo)

Open Serg046 opened this issue 5 years ago • 2 comments

See AcceptMemberVisitor_UnsupportedMemberExpression_Throws

Serg046 avatar Jun 06 '20 13:06 Serg046

                [Fact]
		public void AcceptMemberVisitor_UnsupportedMemberExpression_Throws()
		{
			var factory = new Fake<Expression.InvocationExpression>().Services.Resolve<IMemberVisitorFactory>();
			Expression<Func<DateTime>> expression = () => DateTime.Now;
			var memberExpression = expression.Body as MemberExpression;
			var fake = new Fake<Expression.InvocationExpression>(factory, memberExpression);
			fake.Options.DisableVirtualMembers = true;

			var sut = fake.Rewrite(s => s.AcceptMemberVisitor(new GetValueMemberVisitor(null)));
			sut.Replace((MemberExpression e) => e.Member).Return(new FakeMemberInfo());
			Action act = () => sut.Execute();

			act.Should().Throw<NotSupportedException>("*is not supported*");
		}

                private class FakeMemberInfo : MemberInfo
		{
			public override Type DeclaringType => throw new NotImplementedException();
			public override MemberTypes MemberType => throw new NotImplementedException();
			public override string Name => throw new NotImplementedException();
			public override Type ReflectedType => throw new NotImplementedException();
			public override object[] GetCustomAttributes(bool inherit) => throw new NotImplementedException();
			public override object[] GetCustomAttributes(Type attributeType, bool inherit) => throw new NotImplementedException();
			public override bool IsDefined(Type attributeType, bool inherit) => throw new NotImplementedException();
		}

Serg046 avatar Sep 04 '22 13:09 Serg046

You cannot pass MemberInfo even throug Expression.MakeMemberAccess because it requires either property or field

Serg046 avatar Sep 06 '22 21:09 Serg046