关于 C# 中反射的快速问题
我正在开始使用测试驱动开发的概念,并且有点失败,因为我发现我知道测试会是什么样,但我不知道如何让它做我想要的事情。 我拥有的是一个具有 public
getter 和 internal
setter 的属性。 我想通过从单元测试访问 internal
setter 来测试功能,但我不知道如何做到这一点。 这是测试:
[Test()]
public void HandleInput() {
_requestType = _request.GetType();
PropertyInfo propStdin =
_requestType.GetProperty("StandardInput", BindingFlags.Public | BindingFlags.NonPublic);
if(propStdin == null) {
// Bug in the test.
throw new Exception("There is a bug in the test. Reflection of stdin property returned null.");
}
MethodInfo setStdin = propStdin.GetSetMethod();
// This will fail at the moment since nothing is here to make this happen.
Assert.AreEqual("NewInputNewRequestInput", _request.StandardInput);
}
现在,问题是当我运行测试时,我得到:
[mono-2.4] mbt@zest:~/Projects/StaffASAP/Test.FastCGI/bin/Debug$ nunit-console2 Test.FastCGI.dll
NUnit version 2.4.8
Copyright (C) 2002-2007 Charlie Poole.
Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.
Copyright (C) 2000-2002 Philip Craig.
All Rights Reserved.
Runtime Environment -
OS Version: Unix 2.6.29.0
CLR Version: 2.0.50727.1433 ( Mono 2.4 )
..F
Tests run: 2, Failures: 1, Not run: 0, Time: 0.111 seconds
Test Case Failures:
1) Test.FastCGI.tRequest.HandleInput : System.Exception : There is a bug in the test. Reflection of stdin property returned null.
at Test.FastCGI.tRequest.HandleInput () [0x00051] in /home/mbt/Projects/StaffASAP/Test.FastCGI/tRequest.cs:54
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00057] in /media/disk/mono-2.4/sources/mono-2.4/mcs/class/corlib/System.Reflection/MonoMethod.cs:159
所以,我必须尝试错误地访问该属性,但从查看文档来看,我不知道我做错了什么。 我做错了什么?
I am getting started with the notion of test-driven development, and kind of failing since I am finding that I know what the test is going to be kind of, but I can't figure out how to get it to do what I want. What I have is a property that has a public
getter and an internal
setter. I'd like to test the functionality by accessing the internal
setter from the unit test, but I can't figure out just how to do it. Here is the test:
[Test()]
public void HandleInput() {
_requestType = _request.GetType();
PropertyInfo propStdin =
_requestType.GetProperty("StandardInput", BindingFlags.Public | BindingFlags.NonPublic);
if(propStdin == null) {
// Bug in the test.
throw new Exception("There is a bug in the test. Reflection of stdin property returned null.");
}
MethodInfo setStdin = propStdin.GetSetMethod();
// This will fail at the moment since nothing is here to make this happen.
Assert.AreEqual("NewInputNewRequestInput", _request.StandardInput);
}
Now, the problem is that when I run the test, I get:
[mono-2.4] mbt@zest:~/Projects/StaffASAP/Test.FastCGI/bin/Debug$ nunit-console2 Test.FastCGI.dll
NUnit version 2.4.8
Copyright (C) 2002-2007 Charlie Poole.
Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.
Copyright (C) 2000-2002 Philip Craig.
All Rights Reserved.
Runtime Environment -
OS Version: Unix 2.6.29.0
CLR Version: 2.0.50727.1433 ( Mono 2.4 )
..F
Tests run: 2, Failures: 1, Not run: 0, Time: 0.111 seconds
Test Case Failures:
1) Test.FastCGI.tRequest.HandleInput : System.Exception : There is a bug in the test. Reflection of stdin property returned null.
at Test.FastCGI.tRequest.HandleInput () [0x00051] in /home/mbt/Projects/StaffASAP/Test.FastCGI/tRequest.cs:54
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00057] in /media/disk/mono-2.4/sources/mono-2.4/mcs/class/corlib/System.Reflection/MonoMethod.cs:159
So, I must be attempting to access the property incorrectly, but from looking at the documentation, I don't know what I am doing wrong. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你失踪了吗| GetProperty(...) 调用中的 BindingFlags.Instance?
不过,通过 InternalsVisibleTo 属性向测试程序公开内部变量会更好,因为您不需要依赖反射,并且如果测试项目是 Visual Studio 中解决方案的一部分,则重构将会传播。
Are you missing | BindingFlags.Instance in the GetProperty(...) call?
It would be much nicer however to expose the internal variables to the test program through the InternalsVisibleTo attribute, as you don't need to rely on reflection, and refactoring will propagate if the test project is part of your solution in Visual Studio.
您可能需要查看
InternalsVisibleTo
程序集属性:You may want to look at the
InternalsVisibleTo
assembly attribute:这是一篇关于 SO 的类似文章,涉及单元测试、内部成员和反射..
Here is a similar article on SO that deals with Unit tests, internal members and reflection..