如何将模拟接口对象传递给主类c#
我有以下代码:如何将模拟接口对象传递给主类c#,c#,unit-testing,interface,moq,C#,Unit Testing,Interface,Moq,我有以下代码: public interface Iinterface { Task<bool> RetrieveFromDataBase(); } public class Class1 : Iinterface { public async Task<bool> RetrieveFromDataBase() { //do something
public interface Iinterface
{
Task<bool> RetrieveFromDataBase();
}
public class Class1 : Iinterface
{
public async Task<bool> RetrieveFromDataBase()
{
//do something
return true;
}
}
public class AnotherClass
{
Class1 c = new Class1();
public AnotherClass(Class1 obj)
{
c = obj;
}
public async Task<bool> ExecuteData()
{
var result = await c.RetrieveFromDataBase();
if (result)
{
//do some calculation
}
return true;
}
}
所做的模拟是成功的,即它不会抛出任何错误。我在这里面临的问题是,当我将这个模拟对象的一个参数传递给构造函数时,它抛出了错误System.InvalidCastException:无法将对象“Castle.Proxies.Iinterface”强制转换为类型“Class1”
我知道它无法将模拟接口转换为具体的类类型。但是有没有一种方法可以纠正这个错误,或者将模拟对象传递给主类呢
非常感谢 您应该将变量
c
声明为i接口
。这是使用接口的优点之一。您应该依赖于契约(接口),而不是具体的实现。接下来,您将不耦合到具体的类
public class AnotherClass
{
Iinterface c; //I removed the default new since it will get assigned in constructor
public AnotherClass(Iinterface obj)
{
c = obj;
}
public async Task<bool> ExecuteData()
{
var result = await c.RetrieveFromDataBase();
if (result)
{
//do some calculation
}
return true;
}
}
公共类另一类
{
Iinterface c;//我删除了默认的new,因为它将在构造函数中赋值
公共另一类(I接口obj)
{
c=obj;
}
公共异步任务ExecuteData()
{
var result=wait c.RetrieveFromDataBase();
如果(结果)
{
//算算
}
返回true;
}
}
这里我面临的问题是,类Class1还有一些其他方法和变量,它们没有在接口中声明
您可以在类1中进行合成,并将着色面作为依赖项移动到类1中。请记住,接口是在单元测试中会被模仿的
public class Class1
{
public TIinterface tinterface{get;private set;}
public Class1(TIinterface interface)
{
tinterface= interface;
}
}
public class YourCustomImplementation:TIinterface
{
public async Task<bool> RetrieveFromDataBase()
{
//do something
return true;
}
}
public class AnotherClass
{
Class1 c = new Class1();
public AnotherClass(Class1 obj)
{
c = obj;
}
public async Task<bool> ExecuteData()
{
var result = await c.tinterface.RetrieveFromDataBase();
if (result)
{
//do some calculation
}
return true;
}
}
公共类1
{
公共接口tinterface{get;private set;}
公共类1(TII接口)
{
tinterface=接口;
}
}
公共类YourCustomImplementation:TIinterface
{
公共异步任务RetrieveFromDataBase()
{
//做点什么
返回true;
}
}
公共类另一类
{
Class1 c=新的Class1();
公共另一类(1类obj)
{
c=obj;
}
公共异步任务ExecuteData()
{
var result=wait c.tinterface.RetrieveFromDataBase();
如果(结果)
{
//算算
}
返回true;
}
}
您应该将变量c
声明为i接口
。这是使用接口的优点之一。您应该依赖于契约(接口),而不是具体的实现。接下来,您将不耦合到具体的类
public class AnotherClass
{
Iinterface c; //I removed the default new since it will get assigned in constructor
public AnotherClass(Iinterface obj)
{
c = obj;
}
public async Task<bool> ExecuteData()
{
var result = await c.RetrieveFromDataBase();
if (result)
{
//do some calculation
}
return true;
}
}
公共类另一类
{
Iinterface c;//我删除了默认的new,因为它将在构造函数中赋值
公共另一类(I接口obj)
{
c=obj;
}
公共异步任务ExecuteData()
{
var result=wait c.RetrieveFromDataBase();
如果(结果)
{
//算算
}
返回true;
}
}
这里我面临的问题是,类Class1还有一些其他方法和变量,它们没有在接口中声明
您可以在类1中进行合成,并将着色面作为依赖项移动到类1中。请记住,接口是在单元测试中会被模仿的
public class Class1
{
public TIinterface tinterface{get;private set;}
public Class1(TIinterface interface)
{
tinterface= interface;
}
}
public class YourCustomImplementation:TIinterface
{
public async Task<bool> RetrieveFromDataBase()
{
//do something
return true;
}
}
public class AnotherClass
{
Class1 c = new Class1();
public AnotherClass(Class1 obj)
{
c = obj;
}
public async Task<bool> ExecuteData()
{
var result = await c.tinterface.RetrieveFromDataBase();
if (result)
{
//do some calculation
}
return true;
}
}
公共类1
{
公共接口tinterface{get;private set;}
公共类1(TII接口)
{
tinterface=接口;
}
}
公共类YourCustomImplementation:TIinterface
{
公共异步任务RetrieveFromDataBase()
{
//做点什么
返回true;
}
}
公共类另一类
{
Class1 c=新的Class1();
公共另一类(1类obj)
{
c=obj;
}
公共异步任务ExecuteData()
{
var result=wait c.tinterface.RetrieveFromDataBase();
如果(结果)
{
//算算
}
返回true;
}
}
我希望,我可以将c声明为接口。这里我面临的问题是,类Class1还有一些其他方法和变量,它们没有在接口中声明。这些方法和变量在另一个类中使用。所以当我将它转换为接口时,类有很多编译错误。这是一个给我用来编写测试用例的旧代码:(@CrazyCoder这可以说是一个新问题,我只是在说你提供的解决方案面临的问题。这不是一个新问题。如果Class1做了太多事情,你就打破了单一责任的概念。你能把Class1类分成两个不同的类(或更多)吗因此,您可以将它们作为不同的参数注入构造函数中?您还可以使Class1具有一个TInterface变量,然后在方法调用中使用它RetrieveFromDataBase@Badulake非常感谢您的详细解释和代码。但是您能在我的测试方法中告诉我,从哪里抛出错误,我应该通过模拟吗ed接口对象或转换为Class1并传递它?我希望,我可以将c声明为I接口。这里我面临的问题是,Class1类还有一些其他方法和变量,这些方法和变量没有在接口中声明。这些方法和变量在另一个类中使用。因此,当我将其更改为接口时,该类有很多方法和变量y编译错误。这是我用来编写测试用例的旧代码:(@CrazyCoder这可以说是一个新问题,我只是说你提供的解决方案面临的问题。这不是一个新问题。如果Class1做了太多事情,你就打破了单一责任的概念。你能把Class1分成两个不同的班(或更多)吗因此,您可以将它们作为不同的参数注入构造函数中?您还可以使Class1具有一个TInterface变量,然后在方法调用中使用它RetrieveFromDataBase@Badulake非常感谢您的详细解释和代码。但是您能在我的测试方法中告诉我,从哪里抛出错误,我应该通过模拟吗ed int