-1

こんにちは、Moq を使用して次のメソッドをモックする必要があります。

[HttpPost]
public ActionResult Create(TagsViewModel tagsViewModel)
{
    return RedirectToAction("Index", new { sid = specification.SpecificationId }).Success("Saved Successfully!");
}

しかし、上記の行で例外を受け取りました。問題は、そこにある Success() メソッドで、コードが失敗するモックを作成せずに「System.Web.HttpContext」クラスを利用することです。そこで、HttpContext のモックについて心配する必要がないように、テスト メソッド内で「Success」メソッド自体をモックすることを考えました。それは可能ですか?('Success()' メソッドは、テスト クラスとは別のクラスにあります)

4

1 に答える 1

0

あなたはおそらくこのようなことを試すことができます

[HttpPost]
public ActionResult Create(TagsViewModel tagsViewModel)
{
    var someUnknownObj = TestableRedirectToAction(tagsViewModel);
    return someUnknownObj.Success("Saved Succesfully");        
}

internal someUnknownObjType TestableRedirectToAction(TagsViewModel tagsViewModel)
{
    // ................................
    // ................................
    return RedirectToAction("Index", new { sid = specification.SpecificationId })
}

ここで、すべてのロジックを実行する TestableRedirectToAction メソッドをテストします - Success()

于 2013-02-06T07:06:34.423 に答える