相关疑难解决方法(0)

找到一个有反射的私人领域?

鉴于这门课程

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.那可能吗?

我已经用我寻找属性的属性做了这个,但从来没有私有成员字段.

获取私有字段需要设置哪些绑定标志?

.net c# reflection .net-attributes

219
推荐指数
8
解决办法
15万
查看次数

AutoFixture和私有属性

是否可以指示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)

c# autofixture

10
推荐指数
1
解决办法
1837
查看次数

单元测试访问私有变量

我有一个单元测试课Tester; 我希望它访问Working类的私有字段.

class Working {
    // ...
    private:
    int m_variable;
};

class Tester {
    void testVariable() {
        Working w;
        test( w.m_variable );
    }
}
Run Code Online (Sandbox Code Playgroud)

我有以下选择:

  • 使m_variable public- 丑陋
  • make方法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而不是每个测试......但它不起作用.显然友谊不适用于继承.

这里最优雅的解决方案是什么?

c++ unit-testing

8
推荐指数
3
解决办法
4356
查看次数

使用MSTest测试私有字段

是否可以在单元测试中访问私有字段?

c# mstest

6
推荐指数
1
解决办法
7239
查看次数