鉴于这门课程
class Foo
{
// Want to find _bar with reflection
[SomeAttribute]
private string _bar;
public string BigBar
{
get { return this._bar; }
}
}
Run Code Online (Sandbox Code Playgroud)
我想找到我将用属性标记的私有项_bar.那可能吗?
我已经用我寻找属性的属性做了这个,但从来没有私有成员字段.
获取私有字段需要设置哪些绑定标志?
我是否可以指示AutoFixture还填充私有属性,并使用特定属性(如Ninject.Inject所有类)进行注释?该来源似乎只扫描公共财产:1.这个问题提供了一个特定MyClass的私有设置器的解决方案,但不是私有财产或所有类:2.
我正在使用Moq来模拟服务,最后我想用这些模拟来填充属性.如果我将MyService依赖项公开为,则以下设置可以正常工作public.
一些示例代码:
public class MyController {
[Inject]
private IMyService MyService { get; set; }
public void AMethodUsingMyService() {
MyService.DoSomething();
// ...
}
// ...
}
public class MyService : IMyService {
public void DoSomething()
{
// ...
}
// ...
}
public class MyControllerTest {
[Theory]
[AutoMoqData]
public void MyTest(MyController controller) {
controller.AMethodUsingMyService();
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个单元测试课Tester; 我希望它访问Working类的私有字段.
class Working {
// ...
private:
int m_variable;
};
class Tester {
void testVariable() {
Working w;
test( w.m_variable );
}
}
Run Code Online (Sandbox Code Playgroud)
我有以下选择:
public- 丑陋test_getVariable()- 过于复杂friend class Tester到工作 - 然后明确地工作"知道"测试器,这是不好的我的理想是
class Working {
// ...
private:
int m_variable;
friend class TestBase;
};
class TestBase {};
class Tester : public TestBase {
void testVariable() {
Working w;
test( w.m_variable );
}
}
Run Code Online (Sandbox Code Playgroud)
Working知道TestBase而不是每个测试......但它不起作用.显然友谊不适用于继承.
这里最优雅的解决方案是什么?