moq

Moq requirements? Defeats the purpose?

隐身守侯 提交于 2020-01-02 07:32:45
问题 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? 回答1: 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

Correct method for testing for an exception using Moq and MSTest

六眼飞鱼酱① 提交于 2020-01-02 06:33:53
问题 A little confusion as to the behaviour of Moq with MsTest. Edit: This is not a question of "How do I test?" or "How do I assert?", this is a scratch pad to see how MoQ works so don't focus on the exception type etc. I think a better question may be => "Does Moq Throws<> behave similar to MsTest ExpectedExceptionAttribute?" That is, they're expecting an exception in the test or the SUT? I'd like to know just how MoQ "Throws" works when used with MsTest. Is it better to not use the MsTest

Mocking out expression with NSubstitute

こ雲淡風輕ζ 提交于 2020-01-02 06:11:44
问题 I have a interface that contains the following method signature: TResult GetValue<T, TResult>(object key, Expression<Func<T, TResult>> property) where T : class; Using Moq, I'm able to mock a specific call of this method like this: var repo = new Mock<IRepository>(); repo.Setup(r => r.GetValue<Customer, string>("SomeCustomerId", c => c.SecretAgentId)).Returns("SecretAgentId"); Then when I do this call repo.Object.GetValue<Customer, string>("SomeCustomerId", c => c.SecretAgentId); Tt returns

Nuget Moq 4.0.10827 and InternalsVisibleTo (Again)

给你一囗甜甜゛ 提交于 2020-01-02 05:43:06
问题 I have been trying to get Moq (or rather Castle.Core) to create proxies for my internal types. The following (when added to my project under test), allows things to work: [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] However this (more secure version) does not: [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey

Moq: Unable to cast to interface

这一生的挚爱 提交于 2020-01-02 04:58:05
问题 earlier today I asked this question. So since moq creates it's own class from an interface I wasn't able to cast it to a different class. So it got me wondering what if I created a ICustomPrincipal and tried to cast to that. This is how my mocks look: var MockHttpContext = new Mock<HttpContextBase>(); var MockPrincipal = new Mock<ICustomPrincipal>(); MockHttpContext.SetupGet(h => h.User).Returns(MockPrincipal.Object); In the method I am trying to test the follow code gives the error(again):

How to use Moq to return a List of data or values?

荒凉一梦 提交于 2020-01-02 02:59:07
问题 Can anyone tell me how to return List of data using mock object using Moq framework and assigning the so returned list of data to another List<> variable.?? 回答1: public class SomeClass { public virtual List<int> GimmeSomeData() { throw new NotImplementedException(); } } [TestClass] public class TestSomeClass { [TestMethod] public void HowToMockAList() { var mock = new Mock<SomeClass>(); mock.Setup(m => m.GimmeSomeData()).Returns(() => new List<int> {1, 2, 3}); var resultList = mock.Object

How to modify an invocation parameter of a mocked method with Moq?

无人久伴 提交于 2020-01-02 00:52:13
问题 Is it possible to modify an invocation parameter of a mocked method? In particular I'm looking to change buffer in the following example to a pre-populated byte array. Example: int MockedClass.Read(byte[] buffer, int offset, int count) Explanation: Calling Read loads count bytes reading from offset into the supplied byte array buffer . Now I would like to have buffer populated after the call to Read has been made in my application code. Is that possible? If yes, how would I go about

Mock HostingEnvironment.QueueBackgroundWorkItem in xunit test

眉间皱痕 提交于 2020-01-02 00:42:46
问题 I have a method using HostingEnvironment.QueueBackgroundWorkItem which I wish to unit test some behaviour before this call, however, the test is failing with System.InvalidOperationException : Operation is not valid due to the current state of the object. I suspect this I need to mock the HostingEnvironment but unaware of how to. 回答1: To resolve this issue I defined an interface public interface ITaskScheduler { void QueueBackgroundWorkItem(Action<CancellationToken> workItem); } In production

Moq Mocking with Identity 2.0 Database

给你一囗甜甜゛ 提交于 2020-01-01 12:06:09
问题 I successfully set up an Integration Test using mocking with Moq against my BusAct controller in an MVC 5, Entity Framework 6 app based on a Simple membership database. But now I have migrated the database to Identity 2.0 and replaced the UserProfile with ApplicationUser. IdentityDbContext: (Modified from Simple membership DbContext) public class MyDb : IdentityDbContext<ApplicationUser> // DbContext { public MyDb () : base("MyApplication") { } // public virtual DbSet<UserProfile>

How to mock SoapException using Moq to unit test error handling

末鹿安然 提交于 2020-01-01 12:02:23
问题 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