私は私のBaseApiController
クラスに次のメソッドを持っています。SingleResultと単体テスト
public virtual HttpResponseMessage GetById(int id)
{
var entity = repository.GetById(id);
if (entity == null)
{
var message = string.Format("No {0} with ID = {1}", GenericTypeName, id);
return ErrorMsg(HttpStatusCode.NotFound, message);
}
return Request.CreateResponse(HttpStatusCode.OK, SingleResult.Create(repository.Table.Where(t => t.ID == id)));
}
私はODataの要求のためのSingleResult
を使用しています(私はSingleResultを作成しない場合は、単一のエンティティのための$expand
の作品ではないので)。
しかし、私は具体的なコントローラ(AddressApiControllerなど)でこのメソッドのUnitTestsに問題があります。私はいつも結果にNULL
を得る:
[TestMethod]
public void Get_By_Id()
{
//Arrange
var moq = CreateMockRepository();
var controller = new AddressApiController(moq);
controller.Request = new HttpRequestMessage()
controller.Request.SetConfiguration(new HttpConfiguration())
// Action
HttpResponseMessage response = controller.GetById(1);
var result = response.Content.ReadAsAsync<T>().Result;
// Accert
Assert.IsNotNull(result);
}
私がチェックし、デバッグGetById()
とrepository.Table.Where(t => t.ID == id))
が適切な値を返すことを見つけるが、SingleResult.Create
後、私はNULL
を取得しています。
どうすればこの問題を解決できますか? SingleResultからコンテンツを読み込む方法や他の方法を使用する方法はありますか?
を使用すると、ベースコントローラをテストしていますか?あなたのコードの完全な例をもっと表示してください。 – Andrei
いいえ、私は具体的なコントローラをテストしています。私は質問を更新しました。 – Marusyk
'SingleResult.Create'を追加する前にすべてがうまくいきます。 – Marusyk