草庐IT

assert_difference

全部标签

c# - Debug.Assert 与代码契约(Contract)用法

我应该在什么时候对代码契约进行调试断言,反之亦然?我想检查一种方法的先决条件,但我很困惑要选择一个而不是另一个。我有单元测试,我想在其中测试失败场景并期望出现异常。在同一个方法上使用Debug.Assert和Code契约是一种好的做法吗?如果是这样,代码的编写顺序应该是怎样的?Debug.Assert(parameter!=null);Contract.Requires(parameter!=null,"parameter");或Contract.Requires(parameter!=null,"parameter");Debug.Assert(parameter!=null);背后

c# - 如何使用 Fluent Assertions 来测试不等式测试中的异常?

我正在尝试使用C#中的FluentAssertions为大于覆盖的运算符编写单元测试。如果任一对象为空,则此类中的大于运算符应该抛出异常。通常在使用FluentAssertions时,我会使用lambda表达式将方法放入操作中。然后我会运行操作并使用action.ShouldThrow.但是,我不知道如何将运算符放入lambda表达式中。我不想使用NUnit的Assert.Throws(),Throws约束,或[ExpectedException]属性的一致性。 最佳答案 你可以试试这个方法。[Test]publicvoidGrea

c# - IO 101 : Which are the main differences between TextWriter, FileStream 和 StreamWriter?

如果这个问题对于你们这些经验丰富的程序员来说可能听起来有点业余,让我首先道歉,问题是我在工作中一直有很多关于这个的争论所以我真的想弄清楚这个问题,这就是为什么我我依靠stackoverflow社区一劳永逸地解决这个问题:)因此,一方面,根据MSDN,我们有:TextWriterClassRepresentsawriterthatcanwriteasequentialseriesofcharacters.Thisclassisabstract.FileStreamClassExposesaStreamaroundafile,supportingbothsynchronousandasyn

c# - 为什么 Assert.IsInstanceOfType(0.GetType(), typeof(int)) 会失败?

我是单元测试的新手,使用Microsoft.VisualStudio.TestTools.UnitTesting;0.GetType()其实就是System.RuntimeType,那么我需要写什么样的测试才能通过Assert.IsInstanceOfType(0.GetType(),typeof(int))?---跟进,这是我自己的用户错误...Assert.IsInstanceOfType(0,typeof(int)) 最佳答案 将调用更改为以下内容Assert.IsInstanceOfType(0,typeof(int));第

c# - 错误 : "The specified LINQ expression contains references to queries that are associated with different contexts"

我从LINQ查询中收到标题中显示的错误,该查询包含来自两个不同edmx文件的两个表。这是查询:varquery=(fromaindb1.Table1joinbindb1.Table2ona.Idequalsb.Idorderbya.Statuswhereb.Id==1&&a.Status=="new"selectnew{Id=a.Id,CompanyId=(fromcindb2.Companywheres.Id==a.Idselectnew{c.CompanyId})});db1和db2是与两个不同的edmx文件关联的上下文。我该如何克服这个错误? 最佳答案

c# - NUnit 3.0 和 Assert.Throws

我正在使用NUnit3.0编写一些单元测试,与v2.x不同,ExpectedException()已从库中删除。基于this答案,我绝对可以看到试图在测试中具体捕获人们期望他们的系统抛出异常的地方的逻辑(而不是仅仅说“测试中的任何地方”)。但是,我倾向于非常明确地说明我的Arrange、Act和Assert步骤,这使它成为一个挑战。我曾经做过这样的事情:[Test,ExpectedException(typeof(FormatException))]publicvoidShould_not_convert_from_prinergy_date_time_sample1(){//Arra

c# - Assert.Are Equal 和 Assert.AreSame 之间的区别?

Assert.AreEqual和Assert.AreSame有什么区别? 最佳答案 这意味着AreSame()检查它们是否是完全相同的对象-如果引用指示内存中的相同对象。AreEqual()检查对象是否具有相同的类型和值。相同的对象可以存在于内存中的两个不同位置。 关于c#-Assert.AreEqual和Assert.AreSame之间的区别?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/que

c# - 为什么 Assert.AreEqual(T obj1, Tobj2) 会因相同的字节数组而失败

我在下面的代码段中有两个相同的字节数组://////AtestforBytes///[TestMethod()]publicvoidBytesTest(){byte[]bytes=Encoding.UTF8.GetBytes(Properties.Resources.ExpectedPacketData);TransferEventArgstarget=newTransferEventArgs(bytes);byte[]expected=Encoding.UTF8.GetBytes(Properties.Resources.ExpectedPacketValue);byte[]actu

c# - 使用 Assert 测试异常以确保它们将被抛出的最佳方法

您认为这是测试异常的好方法吗?有什么建议吗?Exceptionexception=null;try{//Imsurethatanexeptionwillhappenhere}catch(Exceptionex){exception=ex;}Assert.IsNotNull(exception);我正在使用MS测试。 最佳答案 我使用了几种不同的模式。大多数情况下,我会在预期会出现异常时使用ExpectedException属性。这足以满足大多数情况,但是,在某些情况下这还不够。异常可能无法捕获——因为它是由反射调用的方法抛出的——或

c# - 错误 : "The node to be inserted is from a different document context"

当我调用XmlNode.AppendChild()时,出现此错误:Thenodetobeinsertedisfromadifferentdocumentcontext.staticpublicXmlNodeXMLNewChildNode(XmlNodeoParent,stringsName,stringsNamespaceURI,stringsNodeType){XmlNodeoNode=moDoc.CreateNode(sNodeType,sName,sNamespaceURI);oParent.AppendChild(oNode);returnoNode;}此代码是从它的VB6.0