moq

How to mock a class without default constructor using Mock.Of<T>()?

久未见 提交于 2019-12-24 11:34:07
问题 Using Moq, I need to create a fake over an existing class (not interface*) that has no default ctor . I can do this using the "traditional" syntax: var fakeResponsePacket = new Mock<DataResponse>( new object[]{0, 0, 0, new byte[0]}); //specify ctor arguments fakeResponsePacket.Setup(p => p.DataLength).Returns(5); var checkResult = sut.Check(fakeResponsePacket.Object); My question is: Is there a way to do the same using the newer Mock.Of<T>() syntax ? From what I can see, there are only two

How to verify order of execution in XUnit Moq?

北城余情 提交于 2019-12-24 10:55:37
问题 I have the following class: public class Foo { public int Prop1 { get; set; } public string Prop2 { get; set; } public Stream TestStream; public WriteToTestStream() { ... } } WriteToTestStream writes to the TestStream . I want to write a Unit Test to make sure that Prop1 and Prop2 are being set before writing to the TestStream The properties should not be accessed once something is written to the TestStream . How can I define a Mock for WriteToTestStream in this case? How can I manipulate

Mocking Context and Repository with UnitOfWork

半城伤御伤魂 提交于 2019-12-24 09:29:46
问题 I am building out unit tests for a small app we need to build. I have implemented the Repository / Unit Of Work pattern. My manager classes implement the unit of work pattern. For a given interface: public interface IUserManager { List<ApplicationUser> GetUsers(Expression<Func<ApplicationUser, bool>> filter = null); ApplicationUser GetUser(Expression<Func<ApplicationUser, bool>> filter); ApplicationUser AddUser(string username, List<string> environmentIds, bool isAdmin = false); void

MVC Mocking (Moq) - HttpContext.Current.Server.MapPath

筅森魡賤 提交于 2019-12-24 08:39:31
问题 I have a method I am attempting to Unit Test which makes use of HttpContext.Current.Server.MapPath as well as File.ReadAllLines as follows: public List<ProductItem> GetAllProductsFromCSV() { var productFilePath = HttpContext.Current.Server.MapPath(@"~/CSV/products.csv"); String[] csvData = File.ReadAllLines(productFilePath); List<ProductItem> result = new List<ProductItem>(); foreach (string csvrow in csvData) { var fields = csvrow.Split(','); ProductItem prod = new ProductItem() { ID =

NullReference durning testing controller with NUnit and Moq

泄露秘密 提交于 2019-12-24 07:35:13
问题 I obtain a NullReference exception in my test. When I comment eventsRepository.AddEvent(eve, User.Identity.GetUserId()); in controller than it goes through. How Can I fix it ? Controller's Method [ValidateAntiForgeryToken] [HttpPost] [Authorize] public ActionResult CreateEvent(Event eve) { if (eve.DateOfBegining < DateTime.Now) { ModelState.AddModelError("DateOfBegining", ""); } if (eve.MaxQuantityOfPlayers < eve.MinCount) { ModelState.AddModelError("MinCount", ""); } if (eve.ConflictSides

Moq.CastleProxyFactory' Could not load file or assembly 'Castle.Core

吃可爱长大的小学妹 提交于 2019-12-24 04:43:04
问题 When i try to setup the the moq object like this: mock.Setup(reader => reader.listOFs(1)).Returns(new List<IIAM_OF_Event>() { new IIAM_OF_Event() { ID = 11 } }.AsQueryable()); It throws System.TypeInitializationException: The type initializer for 'Moq.ProxyFactory' threw an exception. --- System.TypeInitializationException: The type initializer for 'Moq.CastleProxyFactory' threw an exception. ---> System.IO.FileLoadException: Could not load file or assembly 'Castle.Core, Version=4.1.0.0,

Unit test class with one public and multiple private methods

自闭症网瘾萝莉.ら 提交于 2019-12-24 04:01:48
问题 I'm having a little trouble understanding how to approach the following in order to unit test the class. The object under test is an object that consists out of 1 public method, one that accepts a list of objects of type A and returns an object B (which is a binary stream). Due to the nature of the resulting binary stream, which gets large, it is not a nice comparison for the test output. The stream is built using several private instance helper methods. class Foo { private BinaryStream

How to test HttpApplication events in IHttpModules

拥有回忆 提交于 2019-12-24 03:27:59
问题 I am writing HttpModule and need to test it, I am using C# , .NET4.5.2 , NUnit and Moq . Method I am trying to test is Context_BeginRequest : public class XForwardedForRewriter : IHttpModule { public void Init(HttpApplication context) { context.BeginRequest += Context_BeginRequest; } public void Context_BeginRequest(object sender, EventArgs e) { ... } } sender here is HttpApplication and this is where the problems start,... one can create instance of HttpApplication however there is no way to

Problem in casting Generic.List to System.Threading.Task.Generic.List

a 夏天 提交于 2019-12-24 01:17:16
问题 I am trying to create mock of IMemoryCache.TryGetValue method but it returns the following error when it hit cache.Get(cacheKey) : Unable to cast object of type 'System.Collections.Generic.List 1[ConnectionsModel]' to type 'System.Threading.Tasks.Task 1[System.Collections.Generic.List`1[ConnectionsModel] Here is the mock: private static Mock<IMemoryCache> ConfigureMockCacheWithDataInCache(List<ConnectionsModel> auth0ConnectionsResponse) { object value = auth0ConnectionsResponse; var mockCache

MOQ'ing method call sequence

戏子无情 提交于 2019-12-23 21:48:18
问题 I am mocking out a wrapper we use for some enterprise library calls. All was well in the world, my tests passed and the actual code worked! However I then extended the functionality, and verified the tests all still passed. Bingo - they did. However they didn't in the real world because there is an expectation that InitialiseDBCommand(string, commandtype) will be called before AddCmdParameter(string, dbtype, object) So like a good boy, the first thing I want to do is write a test that