moq

ASP.NET MVC Unit Testing - Sessions

ⅰ亾dé卋堺 提交于 2019-12-03 04:52:53
Having searched StackOverflow, and Google I think what I'm doing is suppose to be right, however results don't seem to be going well [TestMethod] public void LoginAction_Should_Return_View_and_User_Authenticated() { // Arrange var mock = new Mock<ControllerContext>(); var mockSession = new Mock<HttpSessionStateBase>(); mock.Setup(p => p.HttpContext.Session).Returns(mockSession.Object); var testData = FakeUserData.CreateTestUsers(); var repository = new FakeUserRepository(testData); var controller = new AccountController(repository); controller.ControllerContext = mock.Object; // Act var result

Why use It.is<> or It.IsAny<> if I could just define a variable?

☆樱花仙子☆ 提交于 2019-12-03 04:45:26
问题 Hi I've been using moq for a while when I see this code. I have to setup a return in one of my repo. mockIRole.Setup(r => r.GetSomething(It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(ReturnSomething); I have three parameters and I just saw these in one of articles or blog on the net. What is the use of It.Is<> or It.IsAny<> for an object? if I could use Guid.NewGuid() or other types then why use It.Is ? I'm sorry I'm not sure if my question is right or am I missing some

Mock.Of<Object> VS Mock<Object>()

一笑奈何 提交于 2019-12-03 04:38:30
I'm currently confuse on how to mock. I'm using Moq. To mock objects I usually write this way var mockIRepo = new Mock<IRepo>(); However, I need to create mock object for my setup. Option1 Is it better to mock my object which only contain properties this way? var object = Mock.Of<Object>() Option2 Or this way var object = new Mock<Object>() I've read that option 2 has setupproperties which is kinda questionable to me because I could also set the properties in option 1. Then what is the difference? Or is there a better way? This post helped me to understand Mock.Of<T> : Old style imperative

Moq fake one method but use real implementation of another

核能气质少年 提交于 2019-12-03 04:14:40
Given an interface IService that has Method1() and Method2() . I want to test that when Method1() throws an Exception , Method2( ) is called and returns a given value . ( Method2() is called when Method1() throws). Therefore I need to test a real Method2() with a fake Method1() , they are methods of the same interface. Here is my test code: MBase sut.MethodX() is the only entry point. It uses IService . My aim is to assert that Method2() returns something . // Arrange // Fake bytes in. var networkStreamMock = new Mock<INetworkStream>(); networkStreamMock.Method1(x => x.Read(It.IsAny<byte[]>(),

How to set up a method twice for different parameters with Moq

廉价感情. 提交于 2019-12-03 04:12:15
I would like to set up a method with Moq twice but it seems that the last one overrides the previous ones. Here's my initial setup: string username = "foo"; string password = "bar"; var principal = new GenericPrincipal( new GenericIdentity(username), new[] { "Admin" }); var membershipServiceMock = new Mock<IMembershipService>(); membershipServiceMock.Setup(ms => ms.ValidateUser(username, password) ).Returns(new ValidUserContext { Principal = principal }); This works out fine but I want this to return new ValidUserContext() if the username or password is different to the username and password

Passing Moq mock-objects to constructor

☆樱花仙子☆ 提交于 2019-12-03 04:09:27
I've been using RhinoMocks for a good while, but just started looking into Moq. I have this very basic problem, and it surprises me that this doesn't fly right out of the box. Assume I have the following class definition: public class Foo { private IBar _bar; public Foo(IBar bar) { _bar = bar; } .. } Now I have a test where I need to Mock the IBar that send to Foo. In RhinoMocks I would simply do it like follows, and it would work just great: var mock = MockRepository.GenerateMock<IBar>(); var foo = new Foo(mock); However, in Moq this doesn't seem to work in the same way. I'm doing as follows:

Mock objects - Setup method - Test Driven Development

孤者浪人 提交于 2019-12-03 04:08:42
问题 I am learning Test Driven Development and trying to use Moq library for mocking. What is the purpose of Setup method of Mock class? 回答1: The default behaviour of a Moq Mock object is to stub all methods and properties. This means that a call to that method/property with any parameters will not fail and will return a default value for the particular return type. You call Setup method for any or all of the following reasons: You want to restrict the input values to the method. public interface

How to mock an SqlDataReader using Moq - Update

给你一囗甜甜゛ 提交于 2019-12-03 03:37:46
问题 I'm new to moq and setting up mocks so i could do with a little help. How do I mock up an SqlDataReader using Moq? Update After further testing this is what I have so far: private IDataReader MockIDataReader() { var moq = new Mock<IDataReader>(); moq.Setup( x => x.Read() ).Returns( true ); moq.Setup( x => x.Read() ).Returns( false ); moq.SetupGet<object>( x => x["Char"] ).Returns( 'C' ); return moq.Object; } private class TestData { public char ValidChar { get; set; } } private TestData

Rhino mock vs Typemock vs JustMock vs [closed]

萝らか妹 提交于 2019-12-03 03:10:09
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I need to choose mock framework to new project. What are the pros and cons for those frameworks? Any comparison table? I know that

Unit test Entity Framework using moq

北城余情 提交于 2019-12-03 02:46:15
I'm using entity framework and trying to unit test my data services which are using EF. I'm not using repository and unit of work patterns. I tried the following approach to mock the context and DbSet: private static Mock<IEFModel> context; private static Mock<IDbSet<CountryCode>> idbSet; [ClassInitialize] public static void Initialize(TestContext testContext) { context = new Mock<IEFModel>(); idbSet = new Mock<IDbSet<CountryCode>>(); context.Setup(c => c.CountryCodes).Returns(idbSet.Object); } I get null "Object reference not set to an instance of an object" error for idbSet "Local". Is there