Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用Moq模拟从属属性_C#_Unit Testing_Moq - Fatal编程技术网

C# 用Moq模拟从属属性

C# 用Moq模拟从属属性,c#,unit-testing,moq,C#,Unit Testing,Moq,如果我有一个类,它有一个通过属性注入解析的依赖项,那么可以使用Moq模拟该属性的行为吗 e、 g 所以,有些类正在测试中,我试图弄清楚我是否可以用Moq模拟IUsefulService的行为,所以当我测试它是可能的,并且使用该服务的行被点击时,模拟被使用…我可能会误解和过于简化这个问题,但我认为下面的代码应该可以工作。由于您将服务属性作为公共属性,因此您可以模拟IUsefulService,新建SomeClass,然后将SomeClass上的服务属性设置为您的模拟 using System; u

如果我有一个类,它有一个通过属性注入解析的依赖项,那么可以使用Moq模拟该属性的行为吗

e、 g


所以,有些类正在测试中,我试图弄清楚我是否可以用Moq模拟IUsefulService的行为,所以当我测试它是可能的,并且使用该服务的行被点击时,模拟被使用…

我可能会误解和过于简化这个问题,但我认为下面的代码应该可以工作。由于您将
服务
属性作为公共属性,因此您可以模拟
IUsefulService
,新建
SomeClass
,然后将
SomeClass
上的
服务
属性设置为您的模拟

using System;
using NUnit.Framework;
using Moq;

namespace MyStuff
{
    [TestFixture]
    public class SomeClassTester
    {
        [Test]
        public void TestIsThisPossible()
        {
            var mockUsefulService = new Mock<IUsefulService>();
            mockUsefulService.Setup(a => a.GetSomethingGood(It.IsAny<object>()))
                .Returns((object input) => string.Format("Mocked something good: {0}", input));

            var someClass = new SomeClass {Service = mockUsefulService.Object};
            Assert.AreEqual("Mocked something good: GOOD!", someClass.IsThisPossible("GOOD!"));
        }
    }

    public interface IUsefulService
    {
        string GetSomethingGood(object theObject);
    }

    public class SomeClass
    {
        //empty constructor
        public SomeClass() { }

        //dependency
        public IUsefulService Service { get; set; }

        public string IsThisPossible(Object someObject)
        {
            //do some stuff

            //I want to mock Service and the result of GetSomethingGood
            var result = Service.GetSomethingGood(someObject);
            return result;
        }
    }
}
使用系统;
使用NUnit.Framework;
使用最小起订量;
名称空间MyStuff
{
[测试夹具]
公共类SomeClassTester
{
[测试]
公共无效测试此可能()
{
var mockUsefulService=new Mock();
mockUsefulService.Setup(a=>a.GetSomethingGood(It.IsAny()))
.Returns((object input)=>string.Format(“Mocked something:{0}”,input));
var someClass=new someClass{Service=mockUsefulService.Object};
AreEqual(“嘲弄好的东西:好的!”,someClass.Isthis可能(“好的!”);
}
}
公共接口IUsefulService
{
字符串GetSomethingGood(对象的对象);
}
公共类
{
//空构造函数
公共类(){}
//依赖性
公共IUsefulService服务{get;set;}
公共字符串是可能的(对象someObject)
{
//做点什么
//我想模拟服务和获得好结果
var result=Service.GetSomethingGood(someObject);
返回结果;
}
}
}

希望有帮助。如果我遗漏了什么,请告诉我,我会看看我能做些什么。

谢谢!这起作用了。我是新来Moq的,似乎找不到一个简单的例子来开始工作。很酷,很高兴这对你有用。Moq快速入门也有一些很好的例子。
using System;
using NUnit.Framework;
using Moq;

namespace MyStuff
{
    [TestFixture]
    public class SomeClassTester
    {
        [Test]
        public void TestIsThisPossible()
        {
            var mockUsefulService = new Mock<IUsefulService>();
            mockUsefulService.Setup(a => a.GetSomethingGood(It.IsAny<object>()))
                .Returns((object input) => string.Format("Mocked something good: {0}", input));

            var someClass = new SomeClass {Service = mockUsefulService.Object};
            Assert.AreEqual("Mocked something good: GOOD!", someClass.IsThisPossible("GOOD!"));
        }
    }

    public interface IUsefulService
    {
        string GetSomethingGood(object theObject);
    }

    public class SomeClass
    {
        //empty constructor
        public SomeClass() { }

        //dependency
        public IUsefulService Service { get; set; }

        public string IsThisPossible(Object someObject)
        {
            //do some stuff

            //I want to mock Service and the result of GetSomethingGood
            var result = Service.GetSomethingGood(someObject);
            return result;
        }
    }
}