Tag: nunit

unit testingHttpContext.Current.Cache或C#中的其他服务器端方法?

为使用HttpContext.Current.Cache类的类创buildunit testing时,使用NUnit时出现错误。 该function是基本的 – 检查一个项目是否在caching中,如果没有,创build它并把它放在: if (HttpContext.Current.Cache["Some_Key"] == null) { myObject = new Object(); HttpContext.Current.Cache.Insert("Some_Key", myObject); } else { myObject = HttpContext.Current.Cache.Get("Some_Key"); } 当从一个unit testing中调用这个NullReferenceException时,遇到第一个Cache行时,它在NullReferenceException时失败。 在Java中,我将使用Cactus来testing服务器端代码。 有没有类似的工具,我可以使用C#代码? 这个SO问题提到模拟框架 – 这是我可以testing这些方法的唯一方法吗? 有没有类似的工具来运行testing的C#? 此外,我不检查Cache是否为空,因为我不想专门为unit testing编写代码,并假定它在服务器上运行时始终有效。 这是否有效,还是应该在caching中添加空检查?

以编程方式跳过一个nunittesting

有没有一种方法让nunittesting结束并告诉testing运行者,应该考虑跳过/忽略,而不是成功或失败? 我的动机是,我有一些testing不适用于某些情况下,但是直到testing(或者灯具)开始运行才能确定。 很明显,在这种情况下,我可以从testing中回来,并允许它成功,但(一)这似乎是错误的,(二)我想知道testing已被跳过。 我知道[忽略]属性,但是这是编译。 我正在寻找一个运行时间,程序的等价物。 就像是: if (testNotApplicable) throw new NUnit.Framework.IgnoreTest("Not applicable"); 或者是以编程方式跳过testing只是错误? 如果是的话,我该怎么做?

nUnit Assert.That(方法,Throws.Exception)不捕获exception

有人能告诉我为什么这个unit testing检查exception失败吗? 显然,我真正的testing是检查其他代码,但我使用Int32.Parse来显示问题。 [Test] public void MyTest() { Assert.That(Int32.Parse("abc"), Throws.Exception.TypeOf<FormatException>()); } testing失败,给出这个错误。 很明显,我正在尝试testing这个exception,我想我的语法错过了一些东西。 Error 1 TestCase '.MyTest' failed: System.FormatException : Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.Int32.Parse(String s) 基于Throws Constraint(NUnit 2.5)的文档,

在NUnittesting中使用WPF组件 – 如何使用STA?

我需要在NUnitunit testing中使用一些WPF组件。 我通过ReSharper运行testing,并且在使用WPF对象时失败,出现以下错误: System.InvalidOperationException: 调用线程必须是STA,因为许多UI组件都需要这个。 我已经读过关于这个问题,听起来像线程需要是STA ,但我还没有想出如何做到这一点。 是什么触发了这个问题是下面的代码: [Test] public void MyTest() { var textBox = new TextBox(); textBox.Text = "Some text"; // <– This causes the exception. }

testing控制器使用User.Identity.Name的操作

我有一个依赖于User.Identity.Name获取当前用户的用户名的行为来获取他的订单列表: public ActionResult XLineas() { ViewData["Filtre"] = _options.Filtre; ViewData["NomesPendents"] = _options.NomesPendents; return View(_repository.ObteLiniesPedido(User.Identity.Name,_options.Filtre,_options.NomesPendents)); } 现在我正在尝试为此编写unit testing,但是我遇到了如何为User.Identity.Name提供一个Mock。 如果我运行我的testing(没有模拟用户…),我得到一个空..exception。 这是正确的方法? 我在想,我的行动代码是不好的unit testing。

如何诊断“TestFixtureSetUp失败”

我们使用TeamCity作为CI服务器,并且在testing失败窗口中刚开始看到"TestFixtureSetUp Failed" 。 任何想法如何去debugging这个问题? testing在我的工作站(VS2008中的R#testing运行器)上运行良好。

NUnit 3.0和Assert.Throws

我正在用NUnit 3.0编写一些unit testing,而不像v2.x, ExpectedException()已经从库中删除了。 基于这个答案,我可以肯定地看到试图特别抓住testing的地方的逻辑,一个人期望他们的系统抛出exception(而不是仅仅说“testing中的任何地方”)。 不过,我倾向于对我的“安排”,“行为”和“断言”步骤非常明确,这使得它成为一个挑战。 我曾经这样做过: [Test, ExpectedException(typeof(FormatException))] public void Should_not_convert_from_prinergy_date_time_sample1() { //Arrange string testDate = "20121123120122"; //Act testDate.FromPrinergyDateTime(); //Assert Assert.Fail("FromPrinergyDateTime should throw an exception parsing invalid input."); } 现在我需要做一些事情: [Test] public void Should_not_convert_from_prinergy_date_time_sample2() { //Arrange string testDate = "20121123120122"; //Act/Assert Assert.Throws<FormatException>(() => testDate.FromPrinergyDateTime()); } 这不是可怕的,但在我看来,这个法案和声明混淆了。 (很明显,对于这个简单的testing,这并不难,但在更大的testing中可能会更具挑战性)。 我有一个同事build议我完全摆脱Assert.Throws ,只是做一些事情: [Test] public void Should_not_convert_from_prinergy_date_time_sample3() { […]

HRESULT:0x80131040:find的程序集清单定义不匹配程序集引用

定位的程序集清单定义与程序集引用不匹配 通过ncover运行nunit时得到这个。 任何想法?

使用Moq来validation呼叫是以正确的顺序进行的

我需要testing下面的方法: CreateOutput(IWriter writer) { writer.Write(type); writer.Write(id); writer.Write(sender); // many more Write()s… } 我创build了一个Moq'd IWriter ,我想确保Write()方法以正确的顺序被调用。 我有以下testing代码: var mockWriter = new Mock<IWriter>(MockBehavior.Strict); var sequence = new MockSequence(); mockWriter.InSequence(sequence).Setup(x => x.Write(expectedType)); mockWriter.InSequence(sequence).Setup(x => x.Write(expectedId)); mockWriter.InSequence(sequence).Setup(x => x.Write(expectedSender)); 但是,在CreateOutput() Write()的第二次调用(写入id值)会引发MockException ,消息“ IWriter.Write()调用失败,模拟行为Strict。模拟上的所有调用都必须具有相应的设置。 ”。 我也很难find任何确定的,最新的Moq序列的文档/例子。 我做错了什么,或者我不能使用相同的方法设置序列? 如果没有,是否有一个替代我可以使用(最好使用Moq / NUnit)?

NUnit:testing没有预期的exception

我想创buildNUnittesting,以确保我的函数不会抛出exception。 有没有一些具体的做法,或者我应该写 [Test] public void noExceptionTest() { testedFunction(); } 如果没有抛出exception,它会成功?