c# - VerifyAll() with Throws<T>() in Moq -
i have mock object method i'm trying setup throwing exception when executed, particular unit test case using moq framework.
var mockmysvc = new mock<imysvc>(); mockmysvc .setup(x=>x.somemethod()) .throws<exception>(); //execution of code //at assertions mockmysvc.verifyall();
at runtime, code complains expections of mockmysvc
not having been met despite exception being thrown. missing or .verifyall()
method not work .throws()
functionality.
i don't know way of setting up, way:
assert.throws<exception>(() => myclass.somemethod());
this way don't need verify anything.
based on comment how make sure exception thrown inside method, can check code inside catch
blocks.
[test] public void test1() { _filmservice.setup(f => f.findbyid(it.isany<int>())).throws<exception>(); _filmcontroller.test(); _filmservice.verify(f => f.exists(it.isany<film>()), times.once); }
actual code:
public actionresult test() { try { _filmservice.findbyid(-1); } catch (system.exception) { _filmservice.exists(null); } return view(); }
this example tested in code , works correctly.
wiki
Comments
Post a Comment