如何测试返回Result的函数?

时间:2015-08-11 21:43:33

标签: testing rust

我有一个返回 <input type="search" class="form-control" data-table="table-striped table-bordered table-hover" id="autocomplete" name="search"> <table class="table table-striped table-bordered table-hover"> </table> <?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "msk"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $query = "select p.productname,p.quantity,p.onesale,p.wholesale,s.fullname from productdetails p , supplier s where s.supplierid = p.supplierID"; $resultt = $conn->query($query); $total_records = mysqli_num_rows($resultt); $per_page=5; if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; } $start_from = ($page-1) * $per_page; $total_pages = ceil($total_records / $per_page); echo "<div class=\"widget-foot\">"; echo "<ul class=\"pagination pagination-sm pull-right\" dir=\"rtl\">"; if($page<$total_pages) { echo "<li><a href='Show_Product.php?page=" . ($page + 1) . "'>Next</a></li>"; } for ($i=$total_pages; $i>=1; $i--) { echo "<li><a href='Show_Product.php?page=".$i."'>".$i."</a></li>"; }; if($page>1){ echo "<li><a href='Show_Product.php?page=".($page-1)."'>Previous</a></li>"; } echo"</ul>"; echo"<div class=\"clearfix\"></div>"; $conn ->close(); ?> 的函数,其中:

Result<(), MyError>

我目前正在做以下事情:

enum MyError {Error1, Error2}

这有效,但看起来很尴尬。起初我打算这样做:

#[test]
fn test_result_function() {
    assert_eq!((), result_function().unwrap());
}

但是当它没有问题时,测试结果并没有在任何地方给出错误。我该如何测试这个功能?

1 个答案:

答案 0 :(得分:3)

怎么样

assert_eq!(Ok(()), result_function());

这需要

#[derive(PartialEq,Debug)]
enum MyError{Error1, Error2}

工作并会告诉你

`(left == right)` (left: `Ok(())`, right: `Err(Error1)`)'

当您的result_function在测试显示它应该返回Error1时返回Ok(())时进行测试。