如何在我的单元测试中模拟这样的代码。我正在使用asp.net核心5中的xUnit和Moq。我是xUnit和Moq的新手。 var url = configuration.GetSection("AppSettings").GetSection("SmsApi").Value; 配置对象已经注入到构造函数中。 这就是我当前在单元测试类中所拥有的 public class UtilityTests
{
private readonly Utility sut;
public UtilityTests()
{
var moc
我有一个类,我正试图通过用C#编写一些单元测试来测试它。我还在我的单元测试中使用Moq库来模拟。
下面是我想要模拟的类中的一些方法,这样它们就可以返回一些我一直想要返回的虚拟数据。
public class DataManager
{
public DataManager(ILoggingService loggingService, IConfiguration configuration)
{
// ...
}
// how to mock this to return dummy data?
private DataHolder
我试图用Moq来模拟MouseButtonEventArgs.GetPosition(),但是我一直收到这个错误:
System.ArgumentException:非覆盖成员上的无效安装程序:M => m.GetPosition(It.IsAny()) at Moq.Mock.ThrowIfCantOverride(表达式设置,MethodInfo methodInfo)在Moq.Mock.<>c__DisplayClass122.<Setup>b\_\_11() at Moq.PexProtector.Invoke<T>(Func1函数处,TRe
Moq.SetupGet抛出异常。
为什么?
如果我没有办法,如何正确地做这件事,如何更改测试的类(我不能添加接口或使属性变为虚拟的或类似的东西)
班级
public class ToDelete
{
public int ToDeleteProperty
{
get;
set;
}
}
测试
[TestMethod]
public void PutCaseByIdCaseContactsExperimental()
{
var toDelete = new Mock<ToDelete>();
toDelete
我正在用Moq框架做一个类的模拟。但是,我无法获取或调用该类的方法。如何在下面的单元测试中解决这个问题?尝试编译程序,在类中提取Moq中的方法。错误列在下面。
类
using System;
using ElectronicsStore.Models;
using Microsoft.Extensions.Logging;
namespace ElectronicsStore.Service
{
public class ParseVendorSupply
{
private readonly ILogger _logger;
public
当我用Moq.AutoMock.Automocker模拟"autoMocker.CreateInstance();“时,我得到了下面的通用消息。HeliumController类的构造函数有几个接口。我如何才能找到哪个特定的接口或构造函数无法模拟?
{System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.NotSupportedException: Type to mock must be an inte
我为一个函数编写单元测试,为此我需要模拟一个实体。我就是这么嘲笑的:
Mock<ISimpleData> data = new Mock<ISimpleData>();
data.Setup(i => i.RiskId.Value).Returns("123");//gets error here
RiskId是StringData : DataGeneric<string>型的。从错误中可以看出,由于RiskId.Value的数据类型,Moq无法获取RiskId。
在我编写测试的函数中使用这个属性时,我还可以模拟RiskId.Valu
我有一节课
public interface IMyInterface
{
string MethodA();
void MethodB();
}
public class MyClass : IMyInterface
{
public string MethodA()
{
// Do something important
}
public void MethodB()
{
string value = MethodA();
// Do something important
我想模拟IncidentData类。
具有构造函数的类:
public class IncidentData
{
public IncidentData(IConfiguration configurationRoot)
{
configurationRoot.GetSection("CarambaAttributeData").Bind(this);
}
}
我试图发送一个类似这样的模拟安装程序:
var incidentDataMock = new Mock<IncidentData>();
incidentDataMoc
我收到了一个旧的项目,并开始重构它的目的。我使用Moq和NUnit框架。我在这个项目中遇到了下一堂课:
public ServerRunner()
{
Name = ConfigurationManager.AppSettings["ServiceName"];
WinService = new ServiceController(Name);
logger = new Logger.Logger(Name);
syncRoot = new ReaderWriterLockSlim();
timeoutMilliseconds = 1
我需要像这样测试简单的类:
public class SomeEntity {
public int Id { get; set; }
public string Name { get; set; }
}
我想使用这个类对象的模拟来编写测试,如下所示:
var someMock = new Moq.Mock<SomeEntity>();
someMock.SetupGet(x => x.Id).Returns(12345678);
someMock.SetupGet(x => x.Name).Returns(It.IsIn(someList));
如您所知,M
在需要模拟内部状态属性的情况下,我很难了解如何测试自定义属性。该属性添加一个扩展方法。
Configuration属性是一个静态属性,在实践中工作良好,但在运行并行测试时不起作用。
一些我所拥有的密码:
public class MyAttribute : Attribute
{
// Store a complex object that cannot be passed into constructor
private static IConfiguration _configuration;
public static IConfiguration
{
在Moq中,我希望断言使用特定参数调用方法TestMethod。当测试失败时,我希望看到一个有用的错误消息,类似于:
TestMethod以意外值X调用参数P,其中Y是预期的。
public interface ITestObject
{
void TestMethod(int parameter);
}
为了举例说明,我可以使用一个手写的模拟来实现这一点,如下所示:
Assert.AreEqual失败了。Expected:<3>。Actual:<2>。TestMethod以参数“实际”的意外值调用。
public class MockTestMetho
我想模拟IServiceCollection,以检查是否使用特定的接口和具体类型调用AddSingleton,使用Nsubstite模拟库和xUnit。
这是我的单元测试:
[Fact]
public checkIfServicesAddedTo_DI()
{
var iServiceCollectionMock = Substitute.For<IServiceCollection>();
var iConfiguration = Substitute.For<IConfiguration>();
MatchServicesManager s