moq

How to verify that method was NOT called in Moq?

别等时光非礼了梦想. 提交于 2019-11-29 18:50:41
How do I verify that method was NOT called in Moq ? Does it have something like AssertWasNotCalled? UPDATE: Starting from Version 3.0, a new syntax can be used: mock.Verify(foo => foo.Execute("ping"), Times.Never()); UPDATE : Since version 3, check the update to the question above or Dann's answer below. Either, make your mock strict so it will fail if you call a method for which you don't have an expect new Mock<IMoq>(MockBehavior.Strict) Or, if you want your mock to be loose, use the .Throws( Exception ) var m = new Mock<IMoq>(MockBehavior.Loose); m.Expect(a => a.moo()).Throws(new Exception(

Moq Expect On IRepository Passing Expression

时光怂恿深爱的人放手 提交于 2019-11-29 18:46:57
问题 I am using this code to verify a behavior of a method I am testing: _repository.Expect(f => f.FindAll(t => t.STATUS_CD == "A")) .Returns(new List<JSOFile>()) .AtMostOnce() .Verifiable(); _repository is defined as: private Mock<IRepository<JSOFile>> _repository; When my test is run, I get this exception: Expression t => (t.STATUS_CD = "A") is not supported. Can someone please tell me how I can test this behavior if I can't pass an expression into the Expect method? Thanks!! 回答1: This is a bit

Need help to understand Moq better

只愿长相守 提交于 2019-11-29 18:45:12
I've been looking at the Moq documentation and the comments are too short for me to understand each of things it can do. The first thing I don't get is It.IsAny<string>(). //example using string Is there an advantage of using this over just putting some value in? I know people say to use this if you don't care about the value, but if you don't care about the value can't you just do "a" or something? This just seems like more typing. Secondly, when would be an example be of when you would not care about the value? I thought Moq needs the value to match up stuff. I don't get what It.Is<> is for

Testing a MVC Controller fails with NULL reference exception

蹲街弑〆低调 提交于 2019-11-29 18:17:05
Below is the setup that I am trying to test. The controller: public ActionResult UpsertStudent(StudentModel studentModel) { try { if (!CheckStudentUpdateForEdit(studentModel)) { return Json(new { result = STUDENT_EXISTS }); } // remaining code removed for brevity } private bool CheckStudentUpdateForEdit(StudentModel studentModel) { var returnVal = true; var existingStudent = _updateStudentManager.GetStudentInfo(studentModel.Id); if (existingStudent.StudentType == "Day Scholar") { returnVal = true; } else { returnVal = false; } return returnVal; } The Test method: public void

Moq to set up a function return based on called times

我的梦境 提交于 2019-11-29 16:32:30
问题 I need to mock an interface to call to MSMQ, is there a way I can use Moq to simulate real MSMQ scenario that there are 10 messages in the queue, I call mocked function 10 times and I can get a pre-defined object, on 11th time I should get a different return value (e.g. null)? 回答1: Moq now has an extension method called SetupSequence() in the Moq namespace which means you can define a distinct return value for each specific call. The general idea is that that you just chain the return values

Cannot seem to moq EF CodeFirst 4.1.Help anyone?

走远了吗. 提交于 2019-11-29 15:43:46
问题 I have been given the task to evaluate codeFirst and possible to use for all our future projects. The evaluation is based on using codeFirst with an existing database. Wondering if it's possible to mock the repository using codeFirst 4.1.(no fakes) The idea is to inject a repository into a service and moq the repository. I have been looking on the net but I have only found an example using fakes.I dont want to use fakes I want to use moq. I think my problem is in the architecture of the DAL.

How to mock Entity Framework 6 Async methods?

三世轮回 提交于 2019-11-29 14:55:16
问题 I am new in mocking. I want to mock up my base repository which is depend on Entity Framework 6 DbContext But I fail. I searched in Google a lot but did not get any sufficient result. At last I got an example at testing with async queries and try to follow but it is worked for me. Here is my code : DbContext : public class TimeSketchContext : DbContext { public virtual DbSet<EmployeeSkill> EmployeeSkill { get; set; } } Base Repository : public class BaseRepository<T> : IRepositoryBase<T>

Moq a function with anonymous type

只愿长相守 提交于 2019-11-29 14:41:55
I'm trying to mock this method Task<TResult> GetResultAsync<TResult>(Func<string, TResult> transformFunc) like this iMock.Setup(m => m.GetResultAsync(It.IsAny<Func<string, object>>())).ReturnsAsync(new { isPair = false }); The method to test doing the call passing an anonymous type to the generic parameter like this instance.GetResultAsync(u => new {isPair = u == "something" }) //dont look at the function return because as generic could have diferent implementations in many case Moq never matches my GetResultAsync method with the parameters sent. I'm using Moq 4 The anonymous type is going to

Experiences using moq with VB.Net

谁说胖子不能爱 提交于 2019-11-29 13:47:40
I really like the moq mocking framework. I've used it on several projects. Unfortunately, one of my customers is demanding we use VB.Net. Not my preference, but hey, .Net is .Net, right? I've heard that moq has some trouble with VB. Is this true? Is so, what sorts of trouble? I would expect it to work fine given the language agnostic nature of .Net. Should I look into using some other mocking framework for use with VB? The main problem of VB.net with regards to mocking frameworks is that, contrary to C#, VB does not have anonymous methods, only Lambda expressions (no way to declare an

mock HttpContext.Current.Server.MapPath using Moq?

时间秒杀一切 提交于 2019-11-29 13:36:02
问题 im unit testing my home controller. This test worked fine until I added a new feature which saves images. The method that’s causing the issue is this below. public static void SaveStarCarCAPImage(int capID) { byte[] capBinary = Motorpoint2011Data.RetrieveCapImageData(capID); if (capBinary != null) { MemoryStream ioStream = new MemoryStream(); ioStream = new MemoryStream(capBinary); // save the memory stream as an image // Read in the data but do not close, before using the stream. using