moq

using moq with asp.net core while unit testing

两盒软妹~` 提交于 2019-12-14 02:23:45
问题 I am trying to write a very simple unit test in asp.net core using moq and xunit. But I am getting a error when I run the unit test. StackTrace: at Moq.Mock 1..ctor(MockBehavior behavior, Object[] args) at Moq.Mock 1..ctor(MockBehavior behavior) at Moq.Mock`1..ctor() Could not load file or assembly 'System.Core, version=4.0.0'. Below is my code and project.json file. { "version": "0.1.0-*", "testRunner": "xunit", "dependencies": { "Moq": "4.5.22", "xunit": "2.2.0-beta2-build3300", "dotnet

Problem with matching setup in Moq

非 Y 不嫁゛ 提交于 2019-12-14 02:20:18
问题 I've been using Moq for the past week or so and haven't had any issues until today. I'm having a problem with getting VerifyAll() to properly match the setup of my mock. I'm currently writing unit tests for my application's API. Here's how the application is structured: API <==> Service <==> DAO <==> Database With this in mind, I'm mocking the service object and then constructing an API object using the mocked service. I've written a number of unit tests already without problem up until now.

How do you create a Moq mock for a Func

六月ゝ 毕业季﹏ 提交于 2019-12-14 01:39:59
问题 I have the following Func method which i need to mock off Func<Owned<ISomeInterface>> someMethod { get; set; } but cant figure out how to mock it off using 'Moq' framework. I have read a similar post on SO but still cant seem to mock it off, it always comes back with Expression is not a method invocation: x => Invoke(x.someMethod ) or A matching constructor for the given arguments was not found on the mocked type. ----> System.MissingMethodException : Constructor on type 'Owned

Ways to mock SignalR Clients.Group in unit test

巧了我就是萌 提交于 2019-12-14 00:25:58
问题 I'm writing mock test cases for SignalR application. I just started with the help of Unit Testing SignalR Applications, but my requirement is little bit different that example shown there. Following is the code I have done after googling. SignalRHub public class HubServer : Hub { [HubMethodName("SendNofication")] public void GetNofication(string message) { try { Clients.Group("Manager").BroadCast(message); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } Unit Test public

How to test OnActionExecuted filter?

你说的曾经没有我的故事 提交于 2019-12-13 21:15:04
问题 So I'm overriding OnActionExecuted in my BaseController class to set the CurrentUser property of a BaseViewModel. I'd like to be able to unit test this, but can't figure out how. Here's the code: protected override void OnActionExecuted(ActionExecutedContext filterContext) { if (filterContext.Result is ViewResult && ((ViewResult)filterContext.Result).ViewData.Model != null) { ((BaseViewModel)((ViewResult)filterContext.Result).ViewData.Model).CurrentUser = CurrentUser; } base.OnActionExecuted

Mocked DbSet not returning an object

人走茶凉 提交于 2019-12-13 18:21:51
问题 I am trying to test an update function by mocking the data using Moq. I am using Entity Framework 6. I can print out a count of the DbSet and it is the expected amount. However, when it tries to select an object, it throws an exception, NullReferenceException: Object reference not set to an instance of an object. Here is my test class which sets up the mocked DbSets and DbContext [TestFixture] public class ProductControllerTest { private ProductController controller; private

How do you Mock an class for a unit test that has a return type but no input parameters

孤者浪人 提交于 2019-12-13 17:37:05
问题 I have a method in code that returns an OrganisationModel. public virtual OrganisationModel GetCurrentUserOrganisation() { var user = DBEntities.AspNetUsers.Find(_userId); if (user.ActiveOrganisation != null) { var org = user.OrganisationUsers.Where(p => p.organisationId == user.ActiveOrganisation).Where(p => p.userId == _userId).FirstOrDefault(); if(org != null) { if (org.Organisation != null) { return org.Organisation.ToModel(); } return null; } else { return null; } } return null; } I want

How to mock a private readonly IList<T> property using moq

那年仲夏 提交于 2019-12-13 15:35:06
问题 I'm trying to mock this list: private readonly IList<MyClass> myList = new List<MyClass>(); using this (as seen here): IList<MyClass> mockList = Builder<MyClass>.CreateListOfSize(5).Build(); mockObj.SetupGet<IEnumerable<MyClass>>(o => o.myList).Returns(stakeHoldersList); However at runtime I get an InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[MyClass]' to type 'System.Collections.ObjectModel.ReadOnlyCollection`1[MyClass]'. What am I doing wrong? 回答1:

Moq Equals Only Works With IEquatable

会有一股神秘感。 提交于 2019-12-13 14:54:14
问题 I'm using the Moq framework for unit tests, and I came across this interesting problem. public interface Bar : IEquatable<Bar> { } [TestClass] public class TestClass { Mock<Bar> a; Mock<Bar> b; public TestClass() { a = new Mock<Bar>(); b = new Mock<Bar>(); a.Setup(bar => bar.Equals(b.Object)).Returns(true); } [TestMethod] public void AssertEqualsTest() { Assert.AreEqual(a.Object, b.Object); //fails } [TestMethod] public void AssertIsTrueTest() { Assert.IsTrue(a.Object.Equals(b.Object)); /

How to mock a Generic Abstract class

☆樱花仙子☆ 提交于 2019-12-13 13:32:51
问题 Assuming I have an Interface IReportBuilderService and concrete class ReportBuilderService e.g. public class ReportBuilderService : IReportBuilderService { } I can start to mock this service with Moq as such Mock<IReportBuilderService> _reportBuilderServiceMock = new Mock<IReportBuilderService>(); And mock expectations etc on the mock class, ok no problems. Question: How do I mock the following method signature? public abstract class ReportBuilder<TReport> where TReport : Report, new() where