dimanche 26 avril 2015

moq and fetching an exception

I'm trying to learn moq test with some simple examples I have

public class CustomerService
{
   private readonly ICustomerRepository _repository;
   public CustomerService(ICustomerRepository repository)
   {
       _repository = repository;
   }

   public void CreateWithMoney(CustomerDTO dto)
   {                  
        var cust = new Customer { FirstName = dto.FirstName, LastName = dto.LastName, FinacialStatus = dto.FinacialStatus };
        if (cust.FinacialStatus < 500)
        {
            throw new NotEnoughMoneyException();                
        }
        _repository.Save(cust);
    }
}



[Test]
public void ThrowExceptionIfMoneyIsLessThan500()
{
    var mockRepo = new Mock<ICustomerRepository>();        
    var mockService = new Mock<CustomerService>(mockRepo.Object);

    mockService.Setup(x => x.CreateWithMoney(It.IsAny<CustomerDTO>()))
                 .Throws<NotImplementedException>();  

}

I dont knwo how to verify that is true and most importantly I cannot run test,

cause I'm getting this error Expected: EntityTest.TDD.NotEnoughMoneyException But was: System.NotSupportedException : Invalid setup on a non-virtual (overridable in VB) member: x => x.CreateWithMoney(It.IsAny())

Aucun commentaire:

Enregistrer un commentaire