, 나는 다음과 같은 작품을 발견했습니다 실패의 원인이 고객 객체를 추가 : 의존성 삽입 (Dependency Injection) - 테스트
public CreateInvoiceResult CreateInvoice(string Code, int qty, string Name)
{
if (string.IsNullOrEmpty(Code) || qty <= 0 || repository.GetByName(Name).ID <= 0)
{
return new CreateInvoiceResult(false);
}
그러나 다음 코드로 변경
가 (에 추가 '를 고객의 Cust ') 테스트가 실패하게됩니까?public CreateInvoiceResult CreateInvoice(string stockCode, int quantity, string customerName)
{
Customer cust = repository.GetByName(Name);
if (string.IsNullOrEmpty(Code) || qty <= 0 || cust.ID <= 0)
{
return new CreateInvoiceResult(false);
}
예 테스트 : 왜 이런 일
는 설명 할 수 제발 내가 어떻게 해결할 수 있습니다?
편집 :
[TestClass]
public class MockCustomerRepositoryDBTests
{
public MockCustomerRepositoryDBTests()
{
IList<Customer> customers = new List<Customer>
{
new Customer { ID = 1, Name = "Jim Smith",
Address = "14 Main Road"},
new Customer { ID = 2, Name = "Alex Smith",
Address = "78 Avanue"},
new Customer { ID = 3, Name = "Paul Brown",
Address = "1 Main Road"}
};
// Mock the CustomerRepositoryDB Repository using Moq
Mock<ICustomerRepository> mockCustomerRepository = new Mock<ICustomerRepository>();
// Return a customer by Name
mockCustomerRepository.Setup(mr => mr.GetByName(
It.IsAny<string>())).Returns((string s) => customers.Where(
x => x.Name == s).Single());
// Complete the setup of the Mock Customer Repository
this.MockCustomerRepository = mockCustomerRepository.Object;
}
public readonly ICustomerRepository MockCustomerRepository;
[TestMethod]
public void stockCodeIsNullOrEmpty()
{
//Arrange
var x = new InvoiceController(MockCustomerRepository);
//Act
bool result = x.CreateInvoice("", 1, "test").Success;
//Assert
Assert.AreEqual(result, false);
}
은 방법 : 올바른 저장소 사용, MOQ를 사용하여 테스트를 업데이트 한 'System.InvalidOperationException이 : 시퀀스가 포함를 어떤 요소'
moq에 대한 별도의 질문을해야합니다. –
원래 질문을 모두 삭제할 필요가 없습니다. :) 내가 다시 돌려 놓을 게다. –