Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.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#_Class_Interface_Moq_Concrete - Fatal编程技术网

c#用Moq模拟具体类的接口成员

c#用Moq模拟具体类的接口成员,c#,class,interface,moq,concrete,C#,Class,Interface,Moq,Concrete,我有一个界面ITransaction,如下所示: public interface ITransaction { DateTime EntryTime { get; } DateTime ExitTime { get; } } public class PaymentTransaction : ITransaction { public virtual DateTime LastPaymentTime {

我有一个界面ITransaction,如下所示:

public interface ITransaction {

        DateTime EntryTime { get; }

        DateTime ExitTime { get; }
}
public class PaymentTransaction : ITransaction {

        public virtual DateTime LastPaymentTime
        {
            get { return DateTime.Now; }
        }

        #region ITransaction Members

        public DateTime EntryTime
        {
            get  { throw new NotImplementedException(); }
        }

        public DateTime ExitTime
        {
            get  { throw new NotImplementedException(); }
        }

        #endregion
}
我有一个派生类PaymentTransaction,如下所示:

public interface ITransaction {

        DateTime EntryTime { get; }

        DateTime ExitTime { get; }
}
public class PaymentTransaction : ITransaction {

        public virtual DateTime LastPaymentTime
        {
            get { return DateTime.Now; }
        }

        #region ITransaction Members

        public DateTime EntryTime
        {
            get  { throw new NotImplementedException(); }
        }

        public DateTime ExitTime
        {
            get  { throw new NotImplementedException(); }
        }

        #endregion
}
我想模拟PaymentTransaction对象的所有三个属性

我尝试了以下方法,但不起作用:

var mockedPayTxn = new Mock<PaymentTransaction>();
mockedPayTxn.SetUp(pt => pt.LastPaymentTime).Returns(DateTime.Now); // This works

var mockedTxn = mockedPayTxn.As<ITransaction>();
mockedTxn.SetUp(t => t.EntryTime).Returns(DateTime.Today); 
mockedTxn.SetUp(t => t.ExitTime).Returns(DateTime.Today); 
var mockedPayTxn=new Mock();
mockedPayTxn.SetUp(pt=>pt.LastPaymentTime)。返回(DateTime.Now);//这很有效
var mockedTxn=mockedPayTxn.As();
SetUp(t=>t.EntryTime).Returns(DateTime.Today);
mockedTxn.SetUp(t=>t.ExitTime).Returns(DateTime.Today);
但是当我注射的时候

(mockedTxn.Object作为PaymentTransaction)

在我正在测试的方法中(因为它只接受PaymentTransaction而不接受ITransaction,我也不能更改它),调试器显示入口时间和出口时间的空引用

我想知道是否有人能帮我


谢谢你的期待。

你在模拟一个具体的类,所以我想你只能模拟虚拟的成员(他们必须是可重写的)。

我唯一能够绕过这个问题的方法(无论哪种方法,感觉都像是黑客)要么做你不想做的事,让具体类上的属性成为虚拟的(即使对于接口实现也是如此),要么在你的类上显式地实现接口。例如:

public DateTime EntryTime
{
  get  { return ((ITransaction)this).EntryTime; }
}

DateTime ITransaction.EntryTime
{
  get { throw new NotImplementedException(); }
}

然后,当您创建模拟时,您可以使用
As()
语法,模拟的行为将与您期望的一样。

我知道我模拟的是一个具体的类,但鉴于进入和退出时间道具是接口成员,并且我将我的具体模拟对象投射到接口,我不需要将这些道具虚拟化。我只是想知道是否还有其他方法可以做到这一点。那么你最终找到答案了吗?你是怎么做到的?