2017-11-23 11 views
2

こんにちは私はApiControllerを実装しているAPIコントローラをテストする必要がありますが、これを行う方法はわかりませんが、UnitTestingの基礎はわかりましたが、私にとっては複雑です。Asp.net IHttpActionResultの戻り値の型とマップされたデータを持つメソッドのユニットテスト

namespace Vidly.Controllers.Api 
{ 
    public class CustomersController : ApiController 
    { 
     private ApplicationDbContext _context; 

     public CustomersController(ApplicationDbContext _context) 
     { 
      _context = new ApplicationDbContext(); 
     } 

     // GET /api/customers 
     public IHttpActionResult GetCustomers(string query = null) 
     { 
      var customersQuery = _context.Customers 
       .Include(c => c.MembershipType); 

      if (!String.IsNullOrWhiteSpace(query)) 
       customersQuery = customersQuery.Where(c => c.Name.Contains(query)); 

      var customerDtos = customersQuery 
       .ToList() 
       .Select(Mapper.Map<Customer, CustomerDto>); 

      return Ok(customerDtos);  
     } 

     // GET /api/customers/1 
     public IHttpActionResult GetCustomer(int id) 
     { 
      var customer = _context.Customers.SingleOrDefault(c => c.Id == id); 

      if (customer == null) 
       return NotFound(); 

      return Ok(Mapper.Map<Customer, CustomerDto>(customer)); 
     } 

     // POST /api/customers 
     [HttpPost] 
     public IHttpActionResult CreateCustomer(CustomerDto customerDto) 
     { 
      if (!ModelState.IsValid) 
       return BadRequest(); 

      var customer = Mapper.Map<CustomerDto, Customer>(customerDto); 
      _context.Customers.Add(customer); 
      _context.SaveChanges(); 

      customerDto.Id = customer.Id; 
      return Created(new Uri(Request.RequestUri + "/" + customer.Id), customerDto); 
     } 

     // PUT /api/customers/1 
     [HttpPut] 
     public IHttpActionResult UpdateCustomer(int id, CustomerDto customerDto) 
     { 
      if (!ModelState.IsValid) 
       return BadRequest(); 

      var customerInDb = _context.Customers.SingleOrDefault(c => c.Id == id); 

      if (customerInDb == null) 
       return NotFound(); 

      Mapper.Map(customerDto, customerInDb); 

      _context.SaveChanges(); 

      return Ok(); 
     } 

     // DELETE /api/customers/1 
     [HttpDelete] 
     public IHttpActionResult DeleteCustomer(int id) 
     { 
      var customerInDb = _context.Customers.SingleOrDefault(c => c.Id == id); 

      if (customerInDb == null) 
       return NotFound(); 

      _context.Customers.Remove(customerInDb); 
      _context.SaveChanges(); 

      return Ok(); 
     } 
    } 
} 

は、どのように、私は起動する必要があります、私はいくつかが必要です。また、私はので、多分誰かがここでは、この

で私を助けることができる、ユニットテストでautomapper使用する方法がわからないことは、私のコントローラであり、 dbcontextを模倣するためのこのようなインタフェースの種類:

public interface IAPICustomerRepository 
{ 

    IHttpActionResult GetCustomers(string query = null); 
    IHttpActionResult GetCustomer(int id); 
    IHttpActionResult CreateCustomer(CustomerDto customerDto); 
    IHttpActionResult UpdateCustomer(int id, CustomerDto customerDto); 
    IHttpActionResult DeleteCustomer(int id); 
} 

おそらく私は嘲笑せずにユニットテストを書くことができます。

UPDATE 私はNkosiの提案で私のコードを編集した後、私はこれらのエラーに

<Error> 
    <Message>An error has occurred.</Message> 
    <ExceptionMessage> 
    An error occurred when trying to create a controller of type 'CustomersController'. Make sure that the controller has a parameterless public constructor. 
    </ExceptionMessage> 
    <ExceptionType>System.InvalidOperationException</ExceptionType> 
    <StackTrace> 
    at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) 
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext() 
    </StackTrace> 
    <InnerException> 
    <Message>An error has occurred.</Message> 
    <ExceptionMessage> 
    Type 'Vidly.Controllers.Api.CustomersController' does not have a default constructor 
    </ExceptionMessage> 
    <ExceptionType>System.ArgumentException</ExceptionType> 
    <StackTrace> 
    at System.Linq.Expressions.Expression.New(Type type) 
at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType) 
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) 
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) 
    </StackTrace> 
    </InnerException> 
    </Error> 

を取得していますそれから私は(私が理解としてパラメータなし)デフォルトコンストラクタを作成し、私は別のエラーが表示されます。

<Error> 
    <Message>An error has occurred.</Message> 
    <ExceptionMessage> 
    Object reference not set to an instance of an object. 
    </ExceptionMessage> 
    <ExceptionType>System.NullReferenceException</ExceptionType> 
    <StackTrace> 
    at Vidly.Controllers.Api.CustomersController.GetCustomers(String query) in C:\Users\Dovydas Petrutis\Desktop\vidly-mvc-5-master\Vidly\Controllers\Api\CustomersController.cs:line 26 
at lambda_method(Closure , Object , Object[]) 
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] 
methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) 
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- 
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- 
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- 
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
at System.Web.Http.Dispatcher.HttpControllerDispatcher <SendAsync>d__1.MoveNext() 
    </StackTrace> 
    </Error> 

今どこに問題がありますか?

+0

最初に、テストのために簡単に置き換えられ/維持され、メンテナンス性が向上するように、必要な機能を公開するサービスを作成します。 automapperは抽象化の背後にカプセル化することができる実装上の懸案事項であり、テスト時には問題ではありません。 – Nkosi

+1

抽象クラスとその実装を依存関係コンテナに登録しましたか?このエラーは、コントローラまたはその依存関係のいずれかを解決する際にエラーが発生したために、フレームワークがコントローラを初期化できないことを意味します。 – Nkosi

+1

ここで依存性注入について読むhttps://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/dependency-injection – Nkosi

答えて

2

まず、テストのために簡単に置き換えられ/維持され、保守性が向上するように、必要な機能を公開するサービスを作成します。

リポジトリに言及したときにあなたは近くにいました。 IHttpActionResultはUIの問題ですので、このようなインターフェースをリファクタリングすることができます。

public interface IAPICustomerRepository { 
    IEnumerable<CustomerDto> GetCustomers(string query = null); 
    CustomerDto GetCustomer(int id); 
    int CreateCustomer(CustomerDto customerDto); 
    CustomerDto UpdateCustomer(int id, CustomerDto customerDto); 
    bool? DeleteCustomer(int id); 
} 

コントローラーは、機能面でよりスリムになり、EF/DbContextまたはAutomapperを気にしなくなりました。

public class CustomersController : ApiController { 
    private IAPICustomerRepository repository; 

    public CustomersController(IAPICustomerRepository repository) { 
     this.repository = repository; 
    } 

    // GET /api/customers 
    public IHttpActionResult GetCustomers(string query = null) { 
     var customerDtos = repository.GetCustomers(query); 
     return Ok(customerDtos); 
    } 

    // GET /api/customers/1 
    public IHttpActionResult GetCustomer(int id) { 
     var customer = repository.GetCustomer(id); 
     if (customer == null) 
      return NotFound(); 

     return Ok(customer); 
    } 

    // POST /api/customers 
    [HttpPost] 
    public IHttpActionResult CreateCustomer([FromBody]CustomerDto customerDto) { 
     if (!ModelState.IsValid) 
      return BadRequest(); 
     var id = repository.CreateCustomer(customerDto); 
     customerDto.Id = id; 
     return Created(new Uri(Request.RequestUri + "/" + id), customerDto); 
    } 

    // PUT /api/customers/1 
    [HttpPut] 
    public IHttpActionResult UpdateCustomer(int id, [FromBody]CustomerDto customerDto) { 
     if (!ModelState.IsValid) 
      return BadRequest(); 

     var updated = repository.UpdateCustomer(id, customerDto); 
     if (updated == null) 
      return NotFound(); 

     return Ok(); 
    } 

    // DELETE /api/customers/1 
    [HttpDelete] 
    public IHttpActionResult DeleteCustomer(int id) { 
     var deleted = repository.DeleteCustomer(id); 
     if (deleted == null) 
      return NotFound(); 

     return Ok(); 
    } 
} 

コントローラは抽象化に依存しているため、コントローラを単独でテストするときに機能をモックできます。

Automapperは抽象化の背後にカプセル化することができる実装上の懸案事項であり、テスト時には問題ではありません。

次の例では、Moqモッキングフレームワークを使用しています。選択したフレームワークを使用することができます。

[TestClass] 
public class CustomersController_Should { 
    [TestMethod] 
    public void GetCustomers() { 
     //Arrange 
     var fakeCustomers = new List<CustomerDto>{ 
      new CustomerDto{ Id = 1 } 
     }; 
     var repository = new Mock<IAPICustomerRepository>(); 
     repository 
      .Setup(_ => _.GetCustomers(It.IsAny<string>())) 
      .Returns(fakeCustomers) 
      .Verifiable(); 

     var controller = new CustomersController(repository.Object); 

     //Act 
     var result = controller.GetCustomers(); 

     //Assert 
     repository.Verify(); 
     //..other assertions 
    } 

    //...Other tests 
} 

元々コントローラにあった機能は、本番環境でのリポジトリの実装にカプセル化されます。

関連する問題