通过lambda来参数NUnit测试

通过lambda来参数NUnit测试

本文介绍了通过lambda来参数NUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆运算符重载的类:



公共静态双[, ]运算符+(矩阵的矩阵,双[,]数组),
公共静态双[,]运营商 - (矩阵的矩阵,双[,]数组),
公共静态双[,]运算符*(矩阵的矩阵,双[,]数组),

有关所有这些我想测试操作数为空。我有,一个 NUnit的测试:



公共无效MatrixOperatorOperandIsNullThrows(Func键<矩阵,双[,],双[,]> OP)
{
矩阵M = NULL;
无功权=新的双[,] {{1,1},{1,1}};
Assert.Throws< ArgumentException的>(()=> OP(男,右));
}



如何传递一个Lambda为每个操作员像 (L,R)=> L + R


解决方案

您可以传递正是这样:

  MatrixOperatorOperandIsNullThrows((L,R)=> L + R); 


I have a class with a bunch of overloaded operators:

public static double[,] operator +(Matrix matrix, double[,] array)
public static double[,] operator -(Matrix matrix, double[,] array)
public static double[,] operator *(Matrix matrix, double[,] array)

For all of them I'd like to test operands for null. I have an NUnit test for that:

public void MatrixOperatorOperandIsNullThrows(Func<Matrix, double[,], double[,]> op)
{
    Matrix m = null;
    var right = new double[,] {{1, 1}, {1, 1}};
    Assert.Throws<ArgumentException>(() => op(m, right));
}

How can I pass a lambda for each operator like (l,r) => l + r ?

解决方案

You can pass exactly that:

MatrixOperatorOperandIsNullThrows((l,r) => l + r);

这篇关于通过lambda来参数NUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:11