moq

Proper way to Mock repository objects for unit tests using Moq and Unity

£可爱£侵袭症+ 提交于 2019-11-28 15:48:26
At my job we are using Moq for mocking and Unity for an IOC container. I am fairly new to this and do not have many resources at work to help me out with determining the best practices I should use. Right now, I have a group of repository interfaces (Ex: IRepository1, IRepository2... IRepository4) that a particular process needs to use to do its job. In the actual code I can determine all of the IRepository objects by using the IOC container and using the RegisterType() method. I am trying to figure out the best way to be able to test the method that needs the 4 mentioned repositories. I was

What are the capabilities of Moq and Rhino.mocks?

让人想犯罪 __ 提交于 2019-11-28 14:36:37
问题 I cannot find a specific feature-by-feature comparison of Moq and Rhino. All the questions are "which do you like better and why", or "here's how you do a simple mock in rhino and how it's done in moq". I cannot find a deep comparison anywhere. I'm aware of the syntax differences, I'm not looking for answers about that. I am looking for a capability comparison . For example: Rhino has Expect.On() for threaded mocking. Can Moq do this? What about Multi-mocking (implementing multiple interfaces

Verifying a specific parameter with Moq

僤鯓⒐⒋嵵緔 提交于 2019-11-28 14:30:55
问题 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

How to verify that method was NOT called in Moq?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 13:52:01
问题 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()); 回答1: 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

Need help to understand Moq better

梦想的初衷 提交于 2019-11-28 13:34:40
问题 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

Testing a MVC Controller fails with NULL reference exception

删除回忆录丶 提交于 2019-11-28 12:56:07
问题 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 =

is it possible to mock/fake an extension method?

北慕城南 提交于 2019-11-28 12:16:19
I'm using a controller extension, and I tried to mock it using FakeItEasy (v 1.7.4) like this: A.CallTo(() => controller.RenderView(A<string>.Ignored,A<object>.Ignored,null)).Returns(""); but I get this error: System.NullReferenceException : Object reference not set to an instance of an object. at System.Object.GetType() at FakeItEasy.Creation.ProxyGeneratorSelector.MethodCanBeInterceptedOnInstance(MethodInfo method, Object callTarget, ref String failReason) at FakeItEasy.Configuration.DefaultInterceptionAsserter.AssertThatMethodCanBeInterceptedOnInstance(MethodInfo method, Object callTarget)

How do I use Moq and DbFunctions in unit tests to prevent a NotSupportedException?

别说谁变了你拦得住时间么 提交于 2019-11-28 11:53:10
I'm currently attempting to run some unit tests on a query that is running through the Entity Framework. The query itself runs without any issues on the live version, but the unit tests are always failing. I've narrowed this down to my usage of DbFunctions.TruncateTime, but I don't know of a way around this to get the unit tests to reflect what is happening on the live server. Here is the method that I am using: public System.Data.DataTable GetLinkedUsers(int parentUserId) { var today = DateTime.Now.Date; var query = from up in DB.par_UserPlacement where up.MentorId == mentorUserId &&

Setup Method With Params Array

…衆ロ難τιáo~ 提交于 2019-11-28 11:52:19
I am developing tests for an application. There's a method that has a params array as a parameter. I have set up the method using Moq but when I run the test, the return value of the method is null, which means it is not being mocked. Here's a code sample: public interface ITicketManager { string GetFirstTicketInQueueIfMatches(params string[] ticketsToMatch); } public class TicketManager : ITicketManager { private Queue<string> ticketQueue = new Queue<string>(); public string GetFirstTicketInQueueIfMatches(params string[] ticketsToMatch) { var firstQueuedTicket = ticketQueue.Peek(); var

The right way to use MOQ setup and returns

早过忘川 提交于 2019-11-28 11:30:19
问题 Im new to MOQ and I am a little confused with the setup method. The example below shows one method that i need to test. The method under test returns the latest time from two dates, so I create two datetime objects and pass them to my function. The part where I'm confused is the returns call. This ignores the logic in my method and returns what I tell it to. For example if i say returns(date2) then the assert passes regardless of the logic. Am I doing something wrong? public virtual DateTime