我正在尝试在我的项目中testing一些exception,我发现的exception之一是SQlException 。 看起来你不能去new SqlException()所以我不知道我怎么能抛出一个exception,尤其是没有以某种方式调用数据库(因为这些是unit testing通常build议不要调用数据库,因为它是慢的) 。 我正在使用NUnit和Moq,但我不知道如何伪造。 回答一些似乎都基于ADO.NET的答案,请注意,我正在使用Linq到Sql。 所以东西就像幕后。 @MattHamilton所要求的更多信息: System.ArgumentException : Type to mock must be an interface or an abstract or non-sealed class. at Moq.Mock`1.CheckParameters() at Moq.Mock`1..ctor(MockBehavior behavior, Object[] args) at Moq.Mock`1..ctor(MockBehavior behavior) at Moq.Mock`1..ctor() 当它尝试模拟时,第一行的post var ex = new Mock<System.Data.SqlClient.SqlException>(); ex.SetupGet(e => e.Message).Returns("Exception message");
使用Moq,看着Callback但我还没有find一个简单的例子来理解如何使用它。 你有一个小的工作片段,清楚地说明如何以及何时使用它?
我在我的控制器操作方法中检查ModelState.IsValid ,创build一个Employee像这样: [HttpPost] public virtual ActionResult Create(EmployeeForm employeeForm) { if (this.ModelState.IsValid) { IEmployee employee = this._uiFactoryInstance.Map(employeeForm); employee.Save(); } // Etc. } 我想用我的unit testing方法使用Moq框架来嘲笑它。 我试图嘲笑它是这样的: var modelState = new Mock<ModelStateDictionary>(); modelState.Setup(m => m.IsValid).Returns(true); 但是在我的unit testing用例中抛出了一个exception。 有人可以帮我从这里出去吗?
我如何validation一个方法被称为Moq一次? Verify()与Verifable()是非常混乱的。
我有一个unit testing,我必须模拟一个非虚方法返回一个布尔types public class XmlCupboardAccess { public bool IsDataEntityInXmlCupboard(string dataId, out string nameInCupboard, out string refTypeInCupboard, string nameTemplate = null) { return IsDataEntityInXmlCupboard(_theDb, dataId, out nameInCupboard, out refTypeInCupboard, nameTemplate); } } 所以我有一个XmlCupboardAccess类的模拟对象,我想在我的testing用例中设置这个方法的模拟,如下所示 [TestMethod] Public void Test() { private string temp1; private string temp2; private Mock<XmlCupboardAccess> _xmlCupboardAccess = new Mock<XmlCupboardAccess>(); _xmlCupboardAccess.Setup(x => x.IsDataEntityInXmlCupboard(It.IsAny<string>(), out temp1, out […]
鉴于以下界面: public interface IFoo { bool Foo(string a, bool b = false); } 试图用Moq来嘲笑它: var mock = new Mock<IFoo>(); mock.Setup(mock => mock.Foo(It.IsAny<string>())).Returns(false); 在编译时给出以下错误: expression式树可能不包含使用可选参数的调用或调用 我发现上面提到的问题是Moq问题清单中的一个增强 ,它似乎被分配到4.5版本(无论何时)。 我的问题是:我应该怎么做才能使上述问题不会很快得到解决呢? 我的select只是要么显式设置可选参数的默认值每次我嘲笑它(哪种破坏首先指定一个点)或创build一个没有bool的重载(就像我会做的在C#4之前)? 还是有人遇到更聪明的方法来解决这个问题?
我被困在这个我不知道如何模拟的代码中: ConfigurationManager.AppSettings["User"]; 我不得不嘲笑ConfigurationManager,但我没有线索,我正在使用Moq 。 有人可以给我一个小费? 谢谢!
Verifiable()的目的是什么? 如果我validation一个Mock并将其保留,它仍然validation设置。 编辑:我正在使用VerifyAll()因此validation的一切的原因。 更改为Verify()只有我的.Verifiable() SetUp被检查。
[TestMethod] public void Home_Message_Display_Unknown_User_when_coockie_does_not_exist() { var context = new Mock<HttpContextBase>(); var request = new Mock<HttpRequestBase>(); context .Setup(c => c.Request) .Returns(request.Object); HomeController controller = new HomeController(); controller.HttpContext = context; //Here I am getting an error (read only). … } 我的基础控制器有一个重载的Initialize,得到这个requestContext。 我正试图通过这一点,但我没有做正确的事情。 protected override void Initialize(System.Web.Routing.RequestContext requestContext) { base.Initialize(requestContext); } 我在哪里可以得到更多关于使用Moq嘲弄我的RequestContext和HttpContext的信息? 我试图嘲笑cookies和一般情况。
public void SubmitMessagesToQueue_OneMessage_SubmitSuccessfully() { var messageServiceClientMock = new Mock<IMessageServiceClient>(); var queueableMessage = CreateSingleQueueableMessage(); var message = queueableMessage[0]; var xml = QueueableMessageAsXml(queueableMessage); messageServiceClientMock.Setup(proxy => proxy.SubmitMessage(xml)).Verifiable(); //messageServiceClientMock.Setup(proxy => proxy.SubmitMessage(It.IsAny<XmlElement>())).Verifiable(); var serviceProxyFactoryStub = new Mock<IMessageServiceClientFactory>(); serviceProxyFactoryStub.Setup(proxyFactory => proxyFactory.CreateProxy()).Returns(essageServiceClientMock.Object); var loggerStub = new Mock<ILogger>(); var client = new MessageClient(serviceProxyFactoryStub.Object, loggerStub.Object); client.SubmitMessagesToQueue(new List<IMessageRequestDTO> {message}); //messageServiceClientMock.Verify(proxy => proxy.SubmitMessage(xml), Times.Once()); messageServiceClientMock.Verify(); } […]