我看到很多关于“嘲弄私人方法”的帖子和问题,但仍然无法使其发挥作用,没有找到真正的答案.
让我们忘记代码的气味,你不应该这样做….
让我们忘记代码的气味,你不应该这样做….
从我所了解的我已经做了以下:
1)创建一个类库“MyMoqSamples”
2)添加了对Moq和NUnit的引用
3)编辑AssemblyInfo文件并添加
[assembly:InternalsVisibleTo(“DynamicProxyGenAssembly2”)]
[assembly:InternalsVisibleTo(“MyMoqSamples”)]
4)现在需要测试一个私有方法,因为它是一种私有方法,它不是接口的一部分.
- [TestFixture]
- public class Can_test_my_private_method
- {
- [Test]
- public void Should_be_able_to_test_my_private_method()
- {
- // TODO how do I test my DoSomthing method?
- }
- }
- public class CustomerInfo
- {
- public string Name { get; set; }
- public string Surname { get; set; }
- }
- public interface ICustomerService
- {
- List<CustomerInfo> GetCustomers();
- }
- public class CustomerService : ICustomerService
- {
- public List<CustomerInfo> GetCustomers()
- {
- return new List<CustomerInfo> { new CustomerInfo { Surname = "Bloggs",Name = "Jo" } };
- }
- protected virtual void DoSomething()
- {
- }
- }
你能给我一个例子,说明你如何测试我的私人方法?
非常感谢
解决方法
您正在描述的步骤设置Moq以测试内部类和成员,因此与测试受保护或私有方法无关
测试私有的方法有点闻起来,你应该只是测试公共API.如果你觉得这个方法真的很重要,并且需要被隔离测试,那么它应该在自己的类中,然后可以自己测试呢?
如果您的心脏设置在测试上面的保护方法,您可以在您的测试组件中滚动您自己的Mock:
- public class CustomerServiceMock : CustomerService {
- public void DoSomethingTester() {
- // Set up state or whatever you need
- DoSomething();
- }
- }
- [TestMethod]
- public void DoSomething_WhenCalled_DoesSomething() {
- CustomerServiceMock serviceMock = new CustomerServiceMock(...);
- serviceMock.DoSomethingTester();
- }
如果是私人的话,你可能会做一些反思的事情,但去路线是测试地狱的方式.
更新
虽然你在你的问题中提供了示例代码,但我并没有真正看到你想如何“测试”受保护的方法,所以我会想出一些有创意的东西…
让我们说你的客户服务如下:
- public CustomerService : ICustomerService {
- private readonly ICustomerRepository _repository;
- public CustomerService(ICustomerRepository repository) {
- _repository = repository;
- }
- public void MakeCustomerPreferred(Customer preferred) {
- MakePreferred(customer);
- _repository.Save(customer);
- }
- protected virtual void MakePreferred(Customer customer) {
- // Or more than likely some grungy logic
- customer.IsPreferred = true;
- }
- }
如果您想测试受保护的方法,您可以执行以下操作:
- [TestClass]
- public class CustomerServiceTests {
- CustomerServiceTester customerService;
- Mock<ICustomerRepository> customerRepositoryMock;
- [TestInitialize]
- public void Setup() {
- customerRepoMock = new Mock<ICustomerRepository>();
- customerService = new CustomerServiceTester(customerRepoMock.Object);
- }
- public class CustomerServiceTester : CustomerService {
- public void MakePreferredTest(Customer customer) {
- MakePreferred(customer);
- }
- // You could also add in test specific instrumentation
- // by overriding MakePreferred here like so...
- protected override void MakePreferred(Customer customer) {
- CustomerArgument = customer;
- WasCalled = true;
- base.MakePreferred(customer);
- }
- public Customer CustomerArgument { get; set; }
- public bool WasCalled { get; set; }
- }
- [TestMethod]
- public void MakePreferred_WithValidCustomer_MakesCustomerPreferred() {
- Customer customer = new Customer();
- customerService.MakePreferredTest(customer);
- Assert.AreEqual(true,customer.IsPreferred);
- }
- // Rest of your tests
- }
这个“模式”的名称是测试特定的子类(基于xUnit测试模式术语),以获取更多信息,您可能希望看到:
http://xunitpatterns.com/Test-Specific%20Subclass.html
根据您的意见和以前的问题,您似乎已经完成了对某些旧代码执行单元测试的任务(或者自己做出决定).在这种情况下,所有遗产代码的圣经是Michael Feathers的书.它涵盖了这样的技术,以及重构和技术来处理将“不可测试”的类和方法分解成更易于管理的东西,我强烈推荐它.