moq

Unit test WebApi2 passing header values

醉酒当歌 提交于 2019-12-04 17:01:50
I am working on a project using WebApi2. With my test project I am using Moq and XUnit. So far testing an api has been pretty straight forward to do a GET like [Fact()] public void GetCustomer() { var id = 2; _customerMock.Setup(c => c.FindSingle(id)) .Returns(FakeCustomers() .Single(cust => cust.Id == id)); var result = new CustomersController(_customerMock.Object).Get(id); var negotiatedResult = result as OkContentActionResult<Customer>; Assert.NotNull(negotiatedResult); Assert.IsType<OkNegotiatedContentResult<Customer>>(negotiatedResult); Assert.Equal(negotiatedResult.Content.Id,id); } Now

Ninject.MockingKernel.Moq security exception

半腔热情 提交于 2019-12-04 16:48:02
I am using Ninject for my IoC container and I'm trying to write some unit tests. I found the Ninject Mocking Kernel so I thought I'd give it a go but I can't get the simplest test to pass. I am missing something and need a little help. Moq.4.0.10827.Final Ninject-2.2.0.0-release-net-4.0 Ninject.MockingKernel-2.2.0.0-release-net-4.0 My unit test... [TestMethod] public void Constructor_CanInitialize() { var kernel = new MoqMockingKernel(); var mock = kernel.Get<IDataRepository>(); <--Error here Assert.IsInstanceOfType(mock, typeof(DataRepository)); } Here is the error... Test method TestFixture

System.NotSupportedException: Invalid setup on a non-virtual (overridable in VB) member

佐手、 提交于 2019-12-04 16:31:57
I am getting a NotSupportedException error message on my Unit Test using Moq System.NotSupportedException: Invalid setup on a non-virtual (overridable in VB) member Unit Test Code: [TestMethod] public void TestEmailNotSentOut() { // ... var dataAccess = new Mock<TjiContext>(); var mockSetStock = new Mock<DbSet<Stock>>(); mockSetStock.As<IQueryable<Stock>>().Setup(m => m.Provider).Returns(stockList.Provider); mockSetStock.As<IQueryable<Stock>>().Setup(m => m.Expression).Returns(stockList.Expression); mockSetStock.As<IQueryable<Stock>>().Setup(m => m.ElementType).Returns(stockList.ElementType);

Ways to mock SignalR Clients.Group in unit test

杀马特。学长 韩版系。学妹 提交于 2019-12-04 16:29:51
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 interface IClientContract { } [TestMethod] public void GetNoficationTest() { var hub = new HubServer(); var

Verifying a delegate was called with Moq

江枫思渺然 提交于 2019-12-04 16:11:05
问题 i got a class that gets by argument a delegate. This class invokes that delegate, and i want to unit test it with Moq. how do i verify that this method was called ? example class : public delegate void Foo(int number); public class A { int a = 5; public A(Foo myFoo) { myFoo(a); } } and I want to check that Foo was called. Thank you. 回答1: What about using an anonymous function? It can act like an inline mock here, you don't need a mocking framework. bool isDelegateCalled = false; var a = new A

Reset mock verification in Moq?

拈花ヽ惹草 提交于 2019-12-04 14:55:50
问题 Setup as so: public interface IFoo { void Fizz(); } [Test] public void A() { var foo = new Mock<IFoo>(MockBehavior.Loose); foo.Object.Fizz(); foo.Verify(x => x.Fizz()); // stuff here foo.Verify(x => x.Fizz(), Times.Never()); // currently this fails } Basically I'd like to enter some code at the // stuff here to make the foo.Verify(x => x.Fizz(), Times.Never()) pass. And because this probably constitutes moq/unit testing abuse, my justification is so I can do something like this: [Test] public

Moq (or possibly another framework) on Mono / MonoTouch

天大地大妈咪最大 提交于 2019-12-04 12:01:46
问题 I've just started some MonoTouch development and I've tried, and failed, to get Moq working for my unit tests. The binary version fails because it's looking for System v2.0, which I assume is down to its Castle requirements, and building it from source crashes the compiler! My question is has anyone gotten Moq to work on Mono (the touch part should be irrelevant, I'm not deploying it to the phone!), or had any joy with any of the other mocking frameworks? Failing that I'm back to rolling my

How to mock ApplicationUserManager from AccountController in MVC5

余生颓废 提交于 2019-12-04 11:33:02
I am trying to write Unit Test for Register Method at AccountController I am using moq and what is the correct way to mock ApplicationUserManager, ApplicationRoleManager and ApplicationSignInManager from Unit Test. public AccountController(ApplicationUserManager userManager, ApplicationRoleManager roleManager, ApplicationSignInManager signInManager) { UserManager = userManager; RoleManager = roleManager; SignInManager = signInManager; } public ApplicationUserManager UserManager { get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); } private set {

Testing: Entity Framework by faking context

∥☆過路亽.° 提交于 2019-12-04 11:23:14
问题 I am eating myself up at this moment. It is like Entity Framework isn't testable. I've read alot of posts and threads where they use unit of work or moq or the repo pattern. I am in a phase that i can't change alot of my architechture of my application. The application fully works at this moment but to be sure I need to have a high code coverage, so testing it is. For testing, I am using the 'fake context' method where I can use the fake one for mocking and the real one for connection to the

How to mock SoapException using Moq to unit test error handling

女生的网名这么多〃 提交于 2019-12-04 11:18:58
I've inherited a small console application that makes calls to a SOAP web service. It's a tragic mess of nested try-catches that log exceptions in various ways, and I'd like to wrap some test coverage around how it behaves when a SoapException gets thrown. Question: How can I mock a class like SoapException using Moq when I can't mock an interface and I can't make properties or methods "virtual"? A little more explanation: To test this error handling, I need to control the Actor property of the SoapException object, as well as the Detail property in order to verify the error handling. A