moq

What would this Moq code look like in RhinoMocks

蓝咒 提交于 2019-12-21 09:26:41
问题 Hey people... trying to get my mocking sorted with asp.net MVC. I've found this example on the net using Moq, basically I'm understanding it to say: when ApplyAppPathModifier is called, return the value that was passed to it. I cant figure out how to do this in Rhino Mocks, any thoughts? var response = new Mock<HttpResponseBase>(); response.Expect(res => res.ApplyAppPathModifier(It.IsAny<string>())) .Returns((string virtualPath) => virtualPath); 回答1: If you are using the stub method as

Mock a method for test

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 08:54:50
问题 Trying to mock a method that is called within another method. public virtual bool hello(string name, int age) { string lastName = GetLastName(); } public virtual string GetLastName() { return "xxx"; } Mock<program> name= new Mock<program>(); name.Setup(x => x.GetLastName()).Returns("qqq"); I want the method GetLastName to always return "qqq". 回答1: This should work, assuming those are the full method implementations public class MyProgram { public bool hello(string name, int age) { string

Mock IMemoryCache in unit test

China☆狼群 提交于 2019-12-21 07:14:03
问题 I am using asp net core 1.0 and xunit. I am trying to write a unit test for some code that uses IMemoryCache . However whenever I try to set a value in the IMemoryCache I get an Null reference error. My unit test code is like this: The IMemoryCache is injected into the class I want to test. However when I try to set a value in the cache in the test I get a null reference. public Test GetSystemUnderTest() { var mockCache = new Mock<IMemoryCache>(); return new Test(mockCache.Object); } [Fact]

Mock a method of the subject under test in Moq?

只谈情不闲聊 提交于 2019-12-21 07:06:41
问题 I want to test method A of my class, but without calling the actual method B which is normally called by A. That's because B has a lot of external interactions I don't want to test for now. I could create mocks for all the services called by B, but that's quite some work. I'd rather just mock B and make it return sample data. Is that possible to do with Moq framework? 回答1: It is, with a catch! You have to make sure method B is virtual and can be overriden. Then, set the mock to call the base

Moq testing LINQ Where queries

送分小仙女□ 提交于 2019-12-21 07:06:36
问题 I'm using EF 4.1 to build a domain model. I have a Task class with a Validate(string userCode) method and in it I want to ensure the user code maps to a valid user in the database, so: public static bool Validate(string userCode) { IDbSet<User> users = db.Set<User>(); var results = from u in users where u.UserCode.Equals(userCode) select u; return results.FirstOrDefault() != null; } I can use Moq to mock IDbSet no problem. But ran into trouble with the Where call: User user = new User {

Unit test protected method in C# using Moq

旧城冷巷雨未停 提交于 2019-12-21 07:06:29
问题 It came to my attention lately that you can unit test abstract base classes using Moq rather than creating a dummy class in test that implements the abstract base class. See How to use moq to test a concrete method in an abstract class? E.g. you can do: public abstract class MyAbstractClass { public virtual void MyMethod() { // ... } } [Test] public void MyMethodTest() { // Arrange Mock<MyAbstractClass> mock = new Mock<MyAbstractClass>() { CallBase = true }; // Act mock.Object.MyMethod(); //

Using Moq can you verify a method call with an anonymous type?

狂风中的少年 提交于 2019-12-21 07:03:45
问题 I'm trying to verify a method call using Moq, but I can't quite get the syntax right. Currently I've go this as my verify: repository.Verify(x => x.ExecuteNonQuery("fav_AddFavorites", new { fid = 123, inputStr = "000456" }), Times.Once()); The code compiles, but the test fails with the error: Expected invocation on the mock once, but was 0 times: x => x.ExecuteNonQuery("fav_AddFavorites", new <>f__AnonymousType0<Int32, String>(123, "000456")) No setups configured. Performed invocations:

how to assert if a method has been called using nunit

て烟熏妆下的殇ゞ 提交于 2019-12-21 06:57:46
问题 is it possible to assert whether a method has been called? I'm testing the following method and I want to assert that the _tokenManager.GetToken() has been called. I just want to know if the method has been called as the method does not return a value. I am using Moq. Thanks, Code snippet public void Subscribe(string code, string emailAddress, string columnKey) { // Request authentication token var token = _tokenManager.GetToken(code, false); if (!_tokenValidator.Validate(token)) { // Token

How can I test that an event contains an event handler?

时光总嘲笑我的痴心妄想 提交于 2019-12-21 05:22:27
问题 I want to test that class A 's RegisterEventHandlers() method registers one of its methods as an EventHandler for an event on class B . How can I do that? I'm using moq, if that matters. I don't think there's a way to inspect the event handler delegate from outside the class (please correct me if I'm wrong). It'd be nice if I could trigger the event and then assert that my callback was called, but if I mock the interface of the A class (and set up an expectation for the callback) then I lose

Moq - Linq expression in repository - Specify expression in setup

不羁的心 提交于 2019-12-21 03:58:09
问题 I have a method on my interface that looks like: T GetSingle(Expression<Func<T, bool>> criteria); I'm trying to mock the setup something like this (I realise this isn't working): _mockUserRepository = new Mock<IRepository<User>>(); _mockUserRepository.Setup(c => c.GetSingle(x => x.EmailAddress == "a@b.com")) .Returns(new User{EmailAddress = "a@b.com"}); I realise I'm passing in the wrong parameter to the setup. After reading this answer I can get it working by passing in the Expression, like