moq

Unit Test Url.Action

北慕城南 提交于 2019-12-06 08:10:45
问题 I am trying to unit test the code I got from an DotNetOpenAuth example but I have a hard time getting the UrlHelper to work in my tests. Somewhere on the LogOn ActionResult on my controller it calls the following UrlHelper. The following example is a simplified version of that ActionResult. public ActionResult TestUrlHelper() { var test = Url.ActionFull("LogOnReturnTo"); return View(); } My test looks something like this: [Test] public void TestTest() { AccountController controller =

how to moq simple add function that uses Unit of Work and Repository Pattern

喜你入骨 提交于 2019-12-06 07:56:03
My test looks like this [Fact] public void SimpleAddTest() { // Arrange var authorizationsToBeAdded = new List<PatientPayerAuthInfo> { new PatientPayerAuthInfo (), new PatientPayerAuthInfo () }.ToList(); var persistentAuthorizations = new List<PatientPayerAuthInfo> { new PatientPayerAuthInfo {PatientPayerAuthInfoId = 1 }, new PatientPayerAuthInfo {PatientPayerAuthInfoId = 2 }, new PatientPayerAuthInfo {PatientPayerAuthInfoId = 3 }, new PatientPayerAuthInfo {PatientPayerAuthInfoId = 4 } }.AsQueryable(); var mockSet = new Mock<DbSet<PatientPayerAuthInfo>>(); mockSet.As<IQueryable

Unit testing with EF Core and in memory database

自古美人都是妖i 提交于 2019-12-06 05:16:39
问题 I am using ASP.NET Core 2.2, EF Core and MOQ. As you can see in the following code, I have two tests, and running both together, with both database name "MovieListDatabase" I got an error in one of the tests with this message: Message: System.ArgumentException : An item with the same key has already been added. Key: 1 If I run each one separately they both pass. And also, having a different database name in both tests, like "MovieListDatabase1" and "MovieListDatabase2" and running both

Moq Unit of Work

社会主义新天地 提交于 2019-12-06 03:09:36
I am new to unit testing and I want to create a test for my search feature. My service layer looks something like: public class EmployeeService: BaseService, IEmployeeService { public EmployeeService(IUnitOfWork unitOfWork) : base(unitOfWork) { _employeeRepo = unitOfWork.EmployeeRepository; } public IEnumerable<Employee> Search(Employee advancedSearch, int[] divisionIds, bool showInactive, int pageSize, int? page) { return _employeeRepo.Search(advancedSearch, divisionIds, showInactive, pageSize, page); } } Unit Test: [Test] public void SearchShouldFilterActiveEmployees() { var employees = new

moqing static method call to c# library class

半城伤御伤魂 提交于 2019-12-06 03:01:57
This seems like an easy enough issue but I can't seem to find the keywords to effect my searches. I'm trying to unit test by mocking out all objects within this method call. I am able to do so to all of my own creations except for this one: public void MyFunc(MyVarClass myVar) { Image picture; ... picture = Image.FromStream(new MemoryStream(myVar.ImageStream)); ... } FromStream is a static call from the Image class (part of c#). So how can I refactor my code to mock this out because I really don't want to provide a image stream to the unit test. You could wrap the static function into Func

Verify property is never set using Moq [duplicate]

余生长醉 提交于 2019-12-06 01:56:24
This question already has answers here : Closed 7 years ago . Possible Duplicate: Moq - How to verify that a property value is set via the setter I would expect the following test to fail: public interface IObjectWithProperty { int Property { get; set; } } [TestMethod] public void Property_ShouldNotBeCalled() { var mock = new Mock<IObjectWithProperty>(); mock.Object.Property = 10; mock.Verify(x => x.Property, Times.Never()); } However, this test passes, even though Property is clearly accessed on the line before the Verify . So it seems that Verify actually means VerifyGet . How should I

Issue with mocking IOrganizationService.Execute in CRM 2011 plugin

偶尔善良 提交于 2019-12-06 01:41:44
I am still new to mocking and I am having trouble with this code: //create the request SendEmailFromTemplateRequest emailUsingTemplateReq = new SendEmailFromTemplateRequest { Target = email, TemplateId = new Guid("07B94C1D-C85F-492F-B120-F0A743C540E6"), RegardingId = toParty[0].PartyId.Id, RegardingType = toParty[0].PartyId.LogicalName }; //retrieve response SendEmailFromTemplateResponse emailUsingTemplateResponse = (SendEmailFromTemplateResponse)service.Execute(emailUsingTemplateReq); var emailId = emailUsingTemplateResponse.Id; I have had no problems up to this point mocking the

Moq requirements? Defeats the purpose?

柔情痞子 提交于 2019-12-06 00:35:55
Doesn't being required to virtualize all property accessors you want to mock kind of defeat the purpose of mocking? I mean, if I have to modify my object and virtualize every single accesor I want to mock, couldn't I just as well inherit my class and mock it myself? Your question is very valid but if you think about it,there is no other way to mock a class. If you take an interface, it's just a contract so the mock framework can mock how ever you want it but if you take a class, it already has an implementation for it's members. So the mock framework, in order to be able to mock the class

Mocking DbEntityEntry

安稳与你 提交于 2019-12-06 00:17:57
I am writing unit tests for my Generic Repository layer but i have some problems with DbEntityEntry . My Update method is like below. public virtual void Update(TEntity entityToUpdate) { var entity = dbSet.Find(context.Entry<ITrackedEntity>(entityToUpdate).Entity.Id); context.Entry(entity).CurrentValues.SetValues(entityToUpdate); } As you can see, context.Entry<TEntity>(TEntity entity) method is invoked by caller. So while unit tests this code throws null exception. I had tried to mock context.Entry method but i couldn't because i couldn't provide return value. My current test method is below

Why do I get a NullReferenceException when testing this async method with MSpec/Moq?

瘦欲@ 提交于 2019-12-06 00:16:22
I want test if the correct type is returned from an async method. This method uses another async method in a dependency class. The dependency class implements this interface: Task<string> DownloadStringAsync(string url); The method I want to test is this: public async Task<T> GetData<T>(string url) where T : class , new() { var jsonData = await _webClientWrapper.DownloadStringAsync(url); if (string.IsNullOrEmpty(jsonData)) return new T(); try { return await JsonConvert.DeserializeObjectAsync<T>(jsonData); } catch (JsonException inner) { throw new JsonConvertException("Error converting Json