moq

Mocking insert query to a MySQL Database using Moq

爱⌒轻易说出口 提交于 2019-12-20 02:45:12
问题 I am currently trying to learn Mocking with Moq, and I wanted to try it on an existing database that I have, however I am unsure how is the correct way to approach this. In my data layer I have a class that handles connecting to the DB and has the various methods for inserting, selecting etc. I want to test whether an actor was correctly inserted into the database. My Insert method currently looks like this: public void Insert(string firstname, string lastname) { string query = $"INSERT INTO

How to mock a method that returns an int with MOQ

自古美人都是妖i 提交于 2019-12-19 23:29:30
问题 I have a class that does some retrieving of contents, and it has a method that requires some inputs (filters) before retrieving it. One of the "input" calls another method, which basically returning an int, how do I mock it using MOQ? Here's an example: namespace MyNamespace { public class ConfigMetaDataColumns : MyModel { public int FieldID { get { return ValueInt("FieldID"); } } public int OrderId { get { return ValueInt("OrderId"); } } public string Label { get { return ValueString("Label"

Moq - It.IsAny<string>() always returning null

折月煮酒 提交于 2019-12-19 12:25:50
问题 What may cause It.IsAny<string>() to return null at every call? Am I incorrect in assuming that it is designed to return a non-null string? Here's the usage - where the Login method throws an ArgumentNullException for a null 2nd argument (connection string). I was assuming that It.IsAny<string>() would provide a non-null string, which would bypass the ArgumentNullException. var mockApiHelper = new Mock<ApiHelper>(); mockApiHelper.Setup(m => m.Connect(It.IsAny<string>(), It.IsAny<string>(), It

Mocking an IoC Container?

ぐ巨炮叔叔 提交于 2019-12-19 10:21:05
问题 Does it make sense to mock an IoC container? If so how would I go about it, using Moq? I am creating a Prism 4 app, using Unity 2.0 as the IoC container. I inject the container into classes that need its services, rather than using Prism's ServiceLocator . For unit testing, unless I need other Prism services for my test, I simply instantiate the container and register mocks with it. I pass the container to the class under test, which resolves the mocks. It's all fairly simple, but I am

How do I raise an event when a method is called using Moq?

橙三吉。 提交于 2019-12-19 05:08:38
问题 I've got an interface like this: public interface IMyInterface { event EventHandler<bool> Triggered; void Trigger(); } And I've got a mocked object in my unit test like this: private Mock<IMyInterface> _mockedObject = new Mock<IMyInterface>(); I want to do something like this: // pseudo-code _mockedObject.Setup(i => i.Trigger()).Raise(i => i.Triggered += null, this, true); However it doesn't look like Raise is available on the ISetup interface that gets returned. How do I do this? 回答1: Your

How to mock httpcontext so that it is not null from a unit test?

两盒软妹~` 提交于 2019-12-19 03:37:20
问题 I am writing a unit test and the controller method is throwing an exception because HttpContext / ControllerContext is null. I don't need to assert anything from the HttpContext, just need it to be not NULL. I have done research and I believe Moq is the answer. But all the samples that I have seen haven't helped me a lot. I don't need to do anything fancy, just to mock the httpcontext. Point me in the right direction! 回答1: Got these two functions from here in a class; public static class

Setup and verify expression with Moq

落花浮王杯 提交于 2019-12-19 02:47:23
问题 Is there a way to setup and verify a method call that use an Expression with Moq? The first attempt is the one I would like to get it to work, while the second one is a "patch" to let the Assert part works (with the verify part still failing) string goodUrl = "good-product-url"; [Setup] public void SetUp() { productsQuery.Setup(x => x.GetByFilter(m=>m.Url== goodUrl).Returns(new Product() { Title = "Good product", ... }); } [Test] public void MyTest() { var controller = GetController(); var

Moq how do you test internal methods?

我只是一个虾纸丫 提交于 2019-12-18 23:37:21
问题 Told by my boss to use Moq and that is it. I like it but it seems that unlike MSTest or mbunit etc... you cannot test internal methods So I am forced to make public some internal implementation in my interface so that i can test it. Am I missing something? Can you test internal methods using Moq? Thanks a lot 回答1: You can use the InternalsVisibleTo attribute to make the methods visible to Moq. http://geekswithblogs.net/MattRobertsBlog/archive/2008/12/16/how-to-make-a-quotprotectedquot-method

Mock HttpContext using moq for unit test [duplicate]

丶灬走出姿态 提交于 2019-12-18 20:29:09
问题 This question already has answers here : How do I mock the HttpContext in ASP.NET MVC using Moq? (5 answers) Closed 5 years ago . I need a mock of HttpContext for unit testing. But I'm struggling with it. I'm making a method that would change sessionId by programmatically with SessionIdManager. And SessionIdManager requires HttpContext not HttpContextBase. But I couldn't find any example to make a mock of HttpContext. All examples out there are only to make HttpContextBase. I tried below but

Mocking out nHibernate QueryOver with Moq

和自甴很熟 提交于 2019-12-18 20:18:35
问题 The following line fails with a null reference, when testing: var awards = _session.QueryOver<Body>().Where(x => x.BusinessId == (int)business).List(); My test is like so: var mockQueryOver = new Mock<IQueryOver<Body, Body>>(); mockQueryOver.Setup(q => q.List()).Returns(new List<Body> {_awardingBody}); _mockSession.Setup(c => c.QueryOver<Body>()).Returns((mockQueryOver.Object)); _mockCommandRunner = new Mock<ICommandRunner>(); _generator = new CertificateGeneratorForOpenSSLCommandLine(