Tag: unit testing模拟

多输入unit testing

我一直在尝试围绕unit testing,我正在尝试处理unit testing一个函数,其返回值取决于一堆参数。 然而,有很多信息,它有点压倒性的.. 考虑以下: 我有一篇Article ,其中有一组价格。 它有一个方法GetCurrentPrice ,它根据一些规则确定当前价格: public class Article { public string Id { get; set; } public string Description { get; set; } public List Prices { get; set; } public Article() { Prices = new List(); } public Price GetCurrentPrice() { if (Prices == null) return null; return ( from […]

模拟一种测试方法

试图模拟在另一个方法中调用的方法。 public virtual bool hello(string name, int age) { string lastName = GetLastName(); } public virtual string GetLastName() { return “xxx”; } Mock name= new Mock(); name.Setup(x => x.GetLastName()).Returns(“qqq”); 我希望方法GetLastName始终返回“qqq”。

如何使用非虚函数对单元进行unit testing模拟

我有一个使用wsdl.exe工具生成的C#类,如下所示: public partial class SoapApi : System.Web.Services.Protocols.SoapHttpClientProtocol { public SOAPTypeEnum AskServerQuestion() { object[] results = return this.Invoke(“AskServerQuestion”); return (SOAPTypeEnum) results[0]; } } 我有一些瘦的包装代码来跟踪结果,等等。是否可以使用任何对象模拟框架来制作一个假的SoapApi类,并为每个对瘦包装函数的调用返回可预测的结果? 我不能将AskServerQuestion()函数设置为虚拟,因为它是由wsdl.exe工具自动生成的。

如何模拟存储库/工作单元

在我的应用程序中,我通过UnitOfWork连接到控制器的通用存储库。 我想对我的应用进行unit testing。 为此,我需要模拟数据库连接。 你能告诉我应该怎么做吗? 模拟回购? 模拟回购和UnitOfWork? 我会感激任何代码片段/建议。 在这里我的回购: public class GenericRepository where TEntity : class { internal EquipmentEntities context; internal DbSet dbSet; public GenericRepository(EquipmentEntities context) { this.context = context; this.dbSet = context.Set(); } public virtual IEnumerable Get( List<Expression<Func>> filter, Func<IQueryable, IOrderedQueryable> orderBy = null, int? Page=0, params Expression<Func>[] included) { IQueryable query = dbSet; […]

如何使用Moq库创建SerialPort模拟?

我必须写很多处理串口的代码。 通常会在电线的另一端连接一个设备,我通常会创建自己的模拟来模拟它们的行为。 我开始关注Moq以帮助进行unit testing。 当你只需要一个存根时,使用它非常简单,但我想知道它是否可行,如果是,我如何为硬件设备创建一个模拟器,根据我想测试的内容做出不同的响应。 一个简单的例子: 我与之接口的设备之一接收命令(移动到位置x),返回ACK消息并进入“移动”状态,直到它到达有序位置。 我想创建一个测试,我发送移动命令,然后继续查询状态,直到它到达最终位置。 我想为两个不同的测试创建两个版本的模拟,一个是我希望设备成功到达最终位置,另一个是失败。 要问的问题太多了?

C# – unit testing,模拟?

应该使用目标类的内置unit testing生成器(VS),还是应该学习如何编写unit testing? 什么是“模拟”的东西? 我一遍又一遍地听到,但没有人愿意给出一个很好的解释。 提前致谢。

拥有受保护的二传手的Moq财产

我想要Moq下一个对象: abstract class Foo { public string Bar { get; protected set; } } 所以new Mock().Bar返回”Blah” 。 我怎样才能做到这一点? fooMock.SetupGet(s => s.Bar).Returns(“Blah”); 投 失败:System.NotSupportedException:非虚拟成员上的设置无效:s => s.Date 和 fooMock.Protected().SetupGet(“Bar”).Returns(“Blah”); 投 要指定公共属性StatementSection.Date的设置,请使用类型化重载

unit testingWeb App时如何模拟应用程序路径

我正在测试MVC HTML帮助程序中的代码,在尝试获取应用程序路径时会抛出错误: //appropriate code that uses System.IO.Path to get directory that results in: string path = “~\\Views\\directory\\subdirectory\\fileName.cshtml”; htmlHelper.Partial(path, model, viewData); //exception thrown here 引发的exception是 System.Web.HttpException:无法使应用程序相对虚拟路径’〜/ Views / directory / subdirectory / fileName.cshtml’成为绝对路径,因为应用程序的路径未知。 遵循如何在测试HtmlHelper时解决图像路径问题的建议? 我假装(使用Moq): Request.Url返回一个字符串 Request.RawUrl返回一个字符串 Request.ApplicationPath返回一个字符串 Request.ServerVariables返回null NameValueCollection Response.ApplyAppPathModifier(string virtualPath)返回一个字符串 还需要什么才能允许此代码在unit testing运行的上下文中运行? 要么 我应该采取什么其他方法来渲染动态构建的字符串的部分视图?

这可以用Moq嘲笑吗?

我正在努力模拟一些外部依赖项,并且遇到一个第三方类的问题,它将构造函数作为另一个第三方类的实例。 希望SO社区可以给我一些指导。 我想创建一个SomeRelatedLibraryClass的模拟实例,它接受它的构造函数SomeLibraryClass的模拟实例。 如何SomeRelatedLibraryClass这种方式模拟SomeRelatedLibraryClass ? 回购代码…… 这是我在测试控制台应用程序中使用的Main方法。 public static void Main() { try { SomeLibraryClass slc = new SomeLibraryClass(“direct to 3rd party”); slc.WriteMessage(“3rd party message”); Console.WriteLine(); MyClass mc = new MyClass(“through myclass”); mc.WriteMessage(“myclass message”); Console.WriteLine(); Mock mockMc = new Mock(“mock myclass”); mockMc.Setup(i => i.WriteMessage(It.IsAny())) .Callback((string message) => Console.WriteLine(string.Concat(“Mock SomeLibraryClass WriteMessage: “, message))); mockMc.Object.WriteMessage(“mock message”); Console.WriteLine(); […]

我如何模拟私人领域?

我真的很嘲笑我正在尝试用模拟对象替换私有字段。 目前,私有字段的实例是在构造函数中创建的。 我的代码看起来像…… public class Cache { private ISnapshot _lastest_snapshot; public ISnapshot LatestSnapshot { get { return this._lastest_snapshot; } private set { this._latest_snapshot = value; } } public Cache() { this.LatestSnapshot = new Snapshot(); } public void Freeze(IUpdates Updates) { ISnapshot _next = this.LastestSnapshot.CreateNext(); _next.FreezeFrom(Updates); this.LastestSnapshot = _next; } } 我要做的是创建一个断言ISnapshot.FreezeFrom(IUpdates)的unit testing从ISnapshot.FreezeFrom(IUpdates)中调用。 我猜我应该用模拟对象替换私有字段_latest_snapshot (可能是错误的假设?)。 如何在保留无参数构造函数的同时不使用LatestSnapshot设置公开? […]