18

私は Web API コントローラーを作成していますが、現在、次のコードがあります。

public class PicklistsController : ApiController
{
    private readonly IPicklistRepository _repository;

    public PicklistsController(IPicklistRepository repository)
    {
        _repository = repository;
    }

    public HttpResponseMessage GetPicklistValues(string entityName, string fieldName)
    {
        if(_repository.Exists(entityName, fieldName))
            return Request.CreateResponse(HttpStatusCode.Accepted, _repository.Get(entityName, fieldName));

        return new HttpResponseMessage(HttpStatusCode.NotFound);
    }

}

私はこのメソッドをテストしようとしていますが、リポジトリがこの値の組み合わせを見つけたときに、HttpResponseMessage に POCO PicklistItem の値が含まれていることを確認するだけです。このフレームワークに慣れていないので、HttpResponseMessage の内部の仕組みがよくわかりません。このサイトで見つけたものや、一般的なグーグル検索では、そのコンテンツでさまざまな ReadAsync メソッドを使用するように指示されていますが、実際にはそうしたくありません。回避できる場合は非同期を使用します。返すオブジェクトに詰め込んだものが、返すときにオブジェクトにあることを確認したいだけです。単体テストでこれまでに行ったことは次のとおりです(JustMockを使用してリポジトリをセットアップし、ターゲットはCUTです):

public void Returns_Picklist_Item_JSON_When_Results_Exist()
{
    Repository.Arrange(repo => repo.Exists(EntityName, FieldName)).Returns(true);

    const int value = 2;
    const string label = "asdf";
    var mynewPicklistItem = new PicklistItem() { Label = label, Value = value };
    Repository.Arrange(repo => repo.Get(EntityName, FieldName)).Returns(Enumerable.Repeat<PicklistItem>(mynewPicklistItem, 1));

    var response = Target.GetPicklistValues(EntityName, FieldName);
    //Assert.IsTrue(I don't know what to do here -- suggestions appreciated);
}

アサートのアイデアはありますか? または、間違ったツリーを吠えていますか、これがどのように機能するかを根本的に誤解していますか? ありがとう...

4

2 に答える 2

16

がオブジェクトの場合Contentは、次のようにキャストしてみてください。プロパティにはオブジェクトが含まれている必要がありますObjectContentValue

もしそうならStreamContent、私はする以外の方法を知りませんReadAsAsync。それでも、タスクの結果をブロックして応答を確認できます。

次に例を示します。

var response = Target.GetPicklistValues(EntityName, FieldName);
ObjectContent objContent = response.Content as ObjectContent;
PicklistItem picklistItem = objContent.Value as PicklistItem;
于 2013-01-31T19:35:50.507 に答える