moq

How could I Mock the FromSql() method?

心不动则不痛 提交于 2019-12-01 02:07:19
问题 I was wondering is there any way other than building a wrapper for mocking the FromSql ? I know this method is static, but since they added things like AddEntityFrameworkInMemoryDatabase to entity framework core, I thought there might be a solution for this too, I use EF Core 1.0.1 in my project. My end goal is to test this method: public List<Models.ClosestLocation> Handle(ClosestLocationsQuery message) { return _context.ClosestLocations.FromSql( "EXEC GetClosestLocations {0}, {1}, {2}, {3}"

Mock HttpClient using Moq

三世轮回 提交于 2019-12-01 01:37:09
问题 I like to unit test a class that use HttpClient . We injected the HttpClient object in the class constructor. public class ClassA : IClassA { private readonly HttpClient _httpClient; public ClassA(HttpClient httpClient) { _httpClient = httpClient; } public async Task<HttpResponseMessage> SendRequest(SomeObject someObject) { //Do some stuff var request = new HttpRequestMessage(HttpMethod.Post, "http://some-domain.in"); //Build the request var response = await _httpClient.SendAsync(request);

How to mock a method call that takes a dynamic object

喜欢而已 提交于 2019-11-30 23:05:37
问题 Say I have the following: public interface ISession { T Get<T>(dynamic filter); } } And I have the following code that I want to test: var user1 = session.Get<User>(new {Name = "test 1"}); var user2 = session.Get<User>(new {Name = "test 2"}); How would I mock this call? Using Moq, I tired doing this: var sessionMock = new Mock<ISession>(); sessionMock.Setup(x => x.Get<User>(new {Name = "test 1")).Returns(new User{Id = 1}); sessionMock.Setup(x => x.Get<User>(new {Name = "test 1")).Returns(new

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

我只是一个虾纸丫 提交于 2019-11-30 21:36:10
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! TheFlyingCircus Got these two functions from here in a class; public static class HttpContextFactory { public static void SetFakeAuthenticatedControllerContext(this Controller

Setup and verify expression with Moq

纵饮孤独 提交于 2019-11-30 20:42:59
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 result = ((ViewResult)controller.Detail(goodUrl)).Model as ProductViewModel; Assert.AreEqual("Good

Why does Autofixture w/ AutoMoqCustomization stop complaining about lack of parameterless constructor when class is sealed?

£可爱£侵袭症+ 提交于 2019-11-30 19:19:24
When I use Moq directly to mock IBuilderFactory and instantiate BuilderService myself in a unit test, I can get a passing test which verifies that the Create() method of IBuilderFactory is called exactly once. However, when I use Autofixture with AutoMoqCustomization , freezing a mock of IBuilderFactory and instantiating BuilderService with fixture.Create<BuilderService> , I get the following exception: System.ArgumentException: Can not instantiate proxy of class: OddBehaviorTests.CubeBuilder. Could not find a parameterless constructor. Parameter name: constructorArguments If I make

Moq how do you test internal methods?

霸气de小男生 提交于 2019-11-30 19:14:27
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 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-available-for-quotpartialquot-mocking-and-again.aspx There is nothing wrong with making the internals

Mocking out nHibernate QueryOver with Moq

只谈情不闲聊 提交于 2019-11-30 18:56:58
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(_mockSession.Object, _mockCommandRunner.Object, _mockDirectory.Object, _mockFile.Object, _mockConfig.Object);

Mock authenticated user using Moq in unit testing

非 Y 不嫁゛ 提交于 2019-11-30 18:47:35
How we can mock the authenticated user using Moq framework. Form Authentication used. I need to write unit tests for the action below public PartialViewResult MyGoals() { int userid = ((SocialGoalUser)(User.Identity)).UserId; var Goals = goalService.GetMyGoals(userid); return PartialView("_MyGoalsView", Goals); } I need to mock the value for userid here I have used something like that, maybe it helps you: var controllerContext = new Mock<ControllerContext>(); var principal = new Moq.Mock<IPrincipal>(); principal.Setup(p => p.IsInRole("Administrator")).Returns(true); principal.SetupGet(x => x

Mock HttpContext using moq for unit test [duplicate]

不打扰是莪最后的温柔 提交于 2019-11-30 18:12:58
This question already has an answer here: How do I mock the HttpContext in ASP.NET MVC using Moq? 5 answers 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 they didn't work HttpContext httpContext = Mock<HttpContext>(); HttpContext httpContext = (HttpContext)GetMockHttpContextBase