public Product GetbyID(int id)
{
try
{
//mycode Product p=..........
}
catch (DataAccessException ex)
{
throw new BusinessException(ex.ErrorCode);
}
catch (Exception ex)
{
BusinessExceptionHandler.LogException(ex);
}
return p;
}
Run Code Online (Sandbox Code Playgroud)
上面给出了我需要编写测试用例的代码片段.这里LogException(ex); 是静态类BusinessExceptionHandler中的静态方法
我参考了Moq框架工作2.6.1014.1
我怎么能用Moq方法BusinessExceptionHandler.LogException
我更喜欢一种不需要对方法GetbyID进行任何更改的模拟机制
我在服务器上使用Web Api OData v4,在客户端上使用OData客户端代码生成器.它工作正常,但我不知道如何在客户端上测试代码.
在服务器上,我公开了一个"Levels"dbSet.
这是客户端上的代码段:
public class LevelViewer
{
public virtual ODataContainer Container{get;set;} //t4 template generated
public LevelViewer(ODataContainer container=null)
{
if(container==null)
{
Container=new ODataContainer(new Uri("http://blabla"));
}
}
//I want to test this (actually there are more things, this is an example)
public List<Level> GetRootLevels()
{
return ODataContainer.Levels.Where(l=>l.IsRoot).ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
我接受由T4模板生成的odata容器作为构造函数的参数,以便能够以某种方式模拟它.
单元测试,这是我丢失的地方:
[TestMethod]
public void LevelsVMConstructorTest()
{
List<Level>levels=new List<Level>();
levels.Add(new Level(){Id=1,LevelId=1,Name="abc",IsRoot=True});
IQueryable<Level>levelsIQ=levels.AsQueryable<Level>();
//?
var odataContainerMock=new Mock<ODataContainer>();
odataContainerMock.Setup(m=>m.Levels).Returns( I DON'T KNOW );
//I want to get here …
Run Code Online (Sandbox Code Playgroud) 我是Unit Testing的新手,我需要在System.IO命名空间中模拟File静态类.我正在使用Rhinomock,实现这一目标的最佳方法是什么,
可以说我需要模拟File.Exists,File.Delete ......
我有一个我用于记录的静态类:
public static class myLogger
{
public static ErrorLogging(string input)
{
//dostuff
}
}
Run Code Online (Sandbox Code Playgroud)
我使用它的方式是:
public class myClassthatDoesStuff
{
...
myLogger.ErrorLogging("some error ocurred");
...
}
Run Code Online (Sandbox Code Playgroud)
如何对myLogger类进行moq 以便能够对其进行单元测试并确保执行ErrorLogging方法?是否可以在构造函数中没有设置任何参数(构造函数注入)的情况下执行此操作? myClassthatDoesStuff要求构造函数中没有参数.
我正在对使用该IMemoryCache
接口的 ClientService 进行单元测试:
客户端服务.cs:
public string Foo()
{
//... code
_memoryCache.Set("MyKey", "SomeValue", new TimeSpan(0, 0, 60));
}
Run Code Online (Sandbox Code Playgroud)
当我尝试模拟IMemoryCache
的Set
扩展名时:
AutoMock mock = AutoMock.GetLoose();
var memoryCacheMock = _mock.Mock<IMemoryCache>();
string value = string.Empty;
// Attempt #1:
memoryCacheMock
.Setup(x => x.Set<string>(It.IsAny<object>(), It.IsAny<string>(), It.IsAny<TimeSpan>()))
.Returns("");
// Attempt #2:
memoryCacheMock
.Setup(x => x.Set(It.IsAny<object>(), It.IsAny<object>(), It.IsAny<TimeSpan>()))
.Returns(new object());
Run Code Online (Sandbox Code Playgroud)
它抛出以下异常:
System.NotSupportedException:不支持的表达式:x => x.Set(It.IsAny(), It.IsAny(), It.IsAny()) 扩展方法(此处:CacheExtensions.Set)不能用于设置/验证前
这是命名空间的缓存扩展 Microsoft.Extensions.Caching.Memory
public static class CacheExtensions
{
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem …
Run Code Online (Sandbox Code Playgroud) c# ×4
moq ×4
unit-testing ×3
nunit ×2
.net ×1
.net-core ×1
mocking ×1
odata ×1
odata-v4 ×1
rhino-mocks ×1