Tag: unit testing

如何在SimpleInjector 2.6.1+中unit testing开放的通用装饰器链

给定以下使用SimpleInjector的开放式通用deocrator链: container.RegisterManyForOpenGeneric(typeof(IHandleQuery), assemblies); container.RegisterDecorator( typeof(IHandleQuery), typeof(ValidateQueryDecorator) ); container.RegisterSingleDecorator( typeof(IHandleQuery), typeof(QueryLifetimeScopeDecorator) ); container.RegisterSingleDecorator( typeof(IHandleQuery), typeof(QueryNotNullDecorator) ); 使用SimpleInjector 2.4.0,我能够使用以下代码对此进行unit testing以断言装饰链: [Fact] public void RegistersIHandleQuery_UsingOpenGenerics_WithDecorationChain() { var instance = Container .GetInstance<IHandleQuery>(); InstanceProducer registration = Container.GetRegistration( typeof(IHandleQuery)); instance.ShouldNotBeNull(); registration.Registration.ImplementationType .ShouldEqual(typeof(HandleFakeQueryWithoutValidator)); registration.Registration.Lifestyle.ShouldEqual(Lifestyle.Transient); var decoratorChain = registration.GetRelationships() .Select(x => new { x.ImplementationType, x.Lifestyle, }) .Reverse().Distinct().ToArray(); decoratorChain.Length.ShouldEqual(3); decoratorChain[0].ImplementationType.ShouldEqual( typeof(QueryNotNullDecorator)); decoratorChain[0].Lifestyle.ShouldEqual(Lifestyle.Singleton); decoratorChain[1].ImplementationType.ShouldEqual( typeof(QueryLifetimeScopeDecorator)); […]

FakeItEasy – 有一个接口伪inheritance自abstract,而两者共享相同的接口inheritance

我有一个界面 public interface IInterface { void DoSomething(); } 另一个界面 public interface IOtherInterface : IInterface { } 一个抽象类 public abstract class AbstractClass : IInterface { public void DoSomething() { Console.WriteLine(“Got here”); } } 我正在编写unit testing并伪造IOtherInterface。 抽象类已经包含了我想要用于unit testing的有用方法。 我如何制作A.Fake(); inheritance自AbstractClass ? 这是我到目前为止尝试但它不起作用 – AbstractClass.DoSomething没有被击中。 IOtherInterface fake = A.Fake(builder => builder.Implements(typeof (AbstractClass))); fake.DoSomething(); 当然,如果我做一个像这样的代理: var abstractFake = A.Fake(); […]

数据驱动unit testing问题

我有一些麻烦让我的unit testing设置为使用Excel .xlsx数据源。 我的App.config文件: 我已经validation它正在找到TestData.xlsx ,并且有一个名为GetAllCellNamesTest的工作表。 在我的unit testing类中,我有以下设置: [TestMethod()] [DeploymentItem(“TestProject\\TestData.xlsx”)] [DataSource(“GetAllCellNamesTest”)] public void GetAllCellNamesTest() { // … test code TestData.xlsx正被复制到测试结果目录中,并且所有不尝试引用数据源的unit testing都将被传递。 但是,这一个测试失败并显示以下消息: The unit test adapter failed to connect to the data source or to read the data. For more information on troubleshooting this error, see “Troubleshooting Data-Driven Unit Tests” (http://go.microsoft.com/fwlink/?LinkId=62412) in the MSDN Library. […]

如何模拟Application.Current进行unit testing?

我有这个: 还有这个: public class GrayscaleEffect : ShaderEffect{ private static PixelShader _pixelShader = new PixelShader() { UriSource = new Uri(@”pack://application:,,,/Effects/GrayscaleEffect.ps”) }; /* … rest of the class … */ } 当我对它进行unit testing( MSTest )时,它显然会引发IOException (因为Application.Current为null,因此pack://application:,,,/…指向无处),并出现此错误: Assembly.GetEntryAssembly() returns null. Set the Application.ResourceAssembly property or use the pack://application:,,,/assemblyname;component/ syntax to specify the assembly to load the resource from. […]

unit testingASP.Net Web应用程序的App_Code

我想创建一个ASP.Net Web应用程序,我想为它编写unit testing。 但我的unit testing项目在我的Web应用程序的App_Code目录中看不到任何类型。 重现步骤 (如果您已经知道如何创建默认的webforms Web应用程序并添加app_code文件夹,请跳过图像) 打开Visual Studio。 选择File-> New-> Project。 选择模板 – > Visual C# – > Web。 选择ASP.Net Web应用程序。 选择“确定”。 选择“Web窗体”。 选中“添加unit testing”。 单击确定。 在解决方案资源管理器中,右键单击Web应用程序,然后选择“添加 – >添加ASP.Net文件夹 – > App_Code”。 将新项添加到App_Code,名为“Util.cs”。 向文件添加一个函数,所以内容是: using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebApplication1.App_Code { public class Util { public static int getMeaningOfLife() […]

如何对转换库进行unit testing?

我刚开始用C#进行unit testing。 我已经阅读了很长一段时间的unit testing,我已经在使用NUnit,但这是我第一次真正尝试为真实代码编写真正的测试。 但我的问题是: 我很难想出能够实际测试的东西。 我想测试的项目是一个转换库(将POCO列表转换为ADO Recordsets)。 到目前为止,我只想测试两件事: 如果记录集完全存在(不是空,不是空) 如果每个字段的内容相同( – >如果RS!Foo == POCO.Foo ) 所以,我的问题是: 当我的代码只将A转换为B时,我还能测试什么? 或者这个项目是否太小/太简单/不是一个很好的例子来编写一些有意义的unit testing?

Assert.AreSame的这种情况应该返回true吗?

我正在测试我创建的存储库模式,并使用Moq包来模拟我的对象。 我想测试来自2个对象的引用,但结果让我感到惊讶。 这是测试: Mock<Repository> moqRepo; Mock<Repository> moqRepo2; public void ObjEqGetTest() { //context is DBContext and has been initialized using [TestInitialize] annotation moqRepo = new Mock<Repository>(context); moqRepo2 = new Mock<Repository>(context); var test = moqRepo.Object.Get(1L); var test2 = moqRepo2.Object.Get(1L); Assert.AreSame(test, test2); } 我的Get方法返回: return entities.SingleOrDefault(predicate) 使用Expression构建器创建predicate (如果需要,我可以添加代码)。 当我创建了两个不同的对象时,为什么这个Assert返回true? 每当您从数据库中获取数据时,Get方法是否返回相同的引用(因为它指向正在使用的模型)? 谢谢你的帮助! 编辑 @CodeCaster说Mocking repos将在我提出的请求中返回null。 但是当我在表Web_Documents中validation值时,Assertions返回true。 让我certificate这一点: public void […]

unit testing仅在构建服务器上运行时失败

为了帮助进行unit testing,我们在委托中包装了DateTime类,以便可以在unit testing中覆盖DateTime.Now 。 public static class SystemTime { #region Static Fields public static Func Now = () => DateTime.Now; #endregion } 以下是在xunitunit testing中使用它的示例: [Fact] public void it_should_update_the_last_accessed_timestamp_on_an_entry() { // Arrange var service = this.CreateClassUnderTest(); var expectedTimestamp = SystemTime.Now(); SystemTime.Now = () => expectedTimestamp; // Act service.UpdateLastAccessedTimestamp(this._testEntry); // Assert Assert.Equal(expectedTimestamp, this._testEntry.LastAccessedOn); } 测试在本地运行正常,但是在我们的构建服务器上失败,因为Assert语句中的日期时间不同。 考虑到DateTime是通过上面提到的委托包装器来模拟的,我正在努力想到它会失败的原因。 我已经validation了UpdateLastAccessedTimestamp方法的实现没有问题,并且测试在本地运行时通过。 […]

继续断言

断言后有什么方法可以继续测试吗? ..我需要查看断言导致的所有情况。 foreach (var ex in data) { Assert.AreEqual(ex1, ex, msg); }

如何测试Windows Phone 7的加速度计?

我想知道测试加速度计的最佳方法是什么? 似乎没有模拟器上的移动模拟,并且嘲弄似乎很难。 我应该做一个最好的猜测,等到真正的设备问世,还是有其他方法可以考虑?