这是我的课程要测试:
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
[ExpectedException(typeof(TaskCanceledException))]
public async Task TestMethod1()
{
var mock = new Mock<MyClass>
{
CallBase = true
};
mock.Protected().Setup<Task>("DoSomethingInternal", new MyContext()).ThrowsAsync(new TaskCanceledException());
var obj = mock.Object;
await obj.DoSomething(null);
}
[TestMethod]
[ExpectedException(typeof(TaskCanceledException))]
public async Task TestMethod2()
{
var mock = new Mock<MyClass>
{
CallBase = true
};
mock.Protected().Setup<Task>("DoSomethingInternal").ThrowsAsync(new TaskCanceledException());
var obj = mock.Object;
await obj.DoSomething();
}
}
}
这是测试代码:
DoSomethingInternal(context)
结果显示,第一个测试失败,第二个成功。调试显示,第一次测试对Task.CompletedTask
的调用返回get
,而不是引发异常。
那我怎么扔呢?
答案 0 :(得分:2)
在设置中传递的参数与执行测试时传递的实例不匹配。在这种情况下,您需要使用参数匹配器,以使代码按预期方式流动。
为受保护成员设置期望,如果需要参数匹配,则必须使用
PHP CODE: <?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "dbname"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $menu= htmlspecialchars($_GET["menu"]); $sql = "select name,menu,price,picture,description from items where menu='$menu' order by menu asc"; $result = $conn->query($sql); $arr=array(); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { array_push($arr, $row); } echo json_encode($arr); } else { echo "0 results"; } $conn->close(); ?> Output: [{"name":"XXXXX","menu":"YYYYY","price":"311.55","picture":"https:\/\/www.google.com\/test\/item\/test.jpg","description":"Test Message"}] Expected Output: { "data":[ { "name":"XXXXX", "menu":"YYYYY", "price":"311.55", "picture":"https:\/\/www.google.com\/test\/item\/test.jpg", "description":"Test Message" } ] }
而不是ItExpr
It