moq

Is there any open source mocking framework resembling TypeMock?

て烟熏妆下的殇ゞ 提交于 2019-12-03 02:36:32
TypeMock is too expensive for a hobbist like me :) Moq or the next version of RhinoMocks have no plans on listening to the profiling API, why is that? EDIT: This enables features such as: Mocking non-virtual methods and properties (!). Mocking browser environments. simpler syntax which is less fragile (and not having to go trough mock objects). Mocking static methods Sometimes is useful (Mostly in legacy scenarios, involving the dreaded DateTime.Now). And more .. TypeMock is too expensive for a hobbist like me It's probably also too expensive to develop and release for free. Declaimer I work

How do I make a unit test to test a method that checks request headers?

天大地大妈咪最大 提交于 2019-12-03 02:29:16
I am very, very new to unit testing and am trying to write a test for a pretty simple method: public class myClass : RequireHttpsAttribute { public override void OnAuthorization(AuthoizationContext filterContext) { var request = filterContext.HttpContext.Request; var header = Convert.ToBoolean(request.Headers["Special-Header-Name"]); if (!(header || request.IsSecureConnection)) { HandleNonHttpsRequest(filterContext); } } } This method, which inherits from the RequireHttpsAttribute , checks if a certain header is present from a page, if it's missing or false, and the page is not secure, then it

Is it possible (with Moq) to stub method calls with Lambda parameters?

不打扰是莪最后的温柔 提交于 2019-12-03 00:32:17
If I do this: var repository = new Mock<IRepository<Banner>>(); repository.Setup(x => x.Where(banner => banner.Is.AvailableForFrontend())).Returns(list); "Where" is a method on my repository that takes a Func<T, ISpecification<T> . AvailableForFrontend returns an implementation of ISpecification, and list is an IEnumberable of the generic type of the repository. It compiles fine, but i get the following error when I run my tests. ---- System.NotSupportedException : Expression banner => Convert((banner.Is.AvailableForFrontend() & banner.Is.SmallMediaBanner())) is not supported. If i use my

MVC 3: How to learn how to test with NUnit, Ninject, and Moq?

好久不见. 提交于 2019-12-03 00:14:23
问题 Short version of my questions: Can anyone point me toward some good, detailed sources from which I can learn how to implement testing in my MVC 3 application, using NUnit, Ninject 2, and Moq? Can anyone here help clarify for me how Controller-Repository decoupling, mocking, and dependency injection work together? Longer version of my questions: What I'm trying to do ... I am currently beginning to create an MVC 3 application, which will use Entity Framework 4, with a database first approach.

Unit testing with Moq, Silverlight and NUnit

百般思念 提交于 2019-12-02 23:58:14
I am attempting to unit test a Silverlight 3 project. I am using: Moq.Silverlight (3.0.308.2) NUnitSilverlight ( http://www.jeff.wilcox.name/2009/01/nunit-and-silverlight/ ) When I write a test that does not use Moq , it works as it should. When I use Moq outside of a test, Moq works as it should. (I mocked a interface and did a verify in a button handler as a proof.) But when I run a unit test that uses Moq, I always get this: System.IO.FileNotFoundException: Could not load file or assembly 'System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies.

How to Unit Test HtmlHelper with Moq?

我只是一个虾纸丫 提交于 2019-12-02 23:26:18
Could somebody show me how you would go about creating a mock HTML Helper with Moq? This article has a link to an article claiming to describe this, but following the link only returns an ASP.NET Runtime Error [edit] I asked a more specific question related to the same subject here , but it hasn't gotten any responses. I figured it was too specific, so I thought I could get a more general answer to a more general question and modify it to meet my requirements. Thanks What you can do is this: HtmlHelper helper = null; helper.YourHelperMethod(); No need to mock anything. Works brilliant for me.

How to Unit Test a custom ModelBinder using Moq?

血红的双手。 提交于 2019-12-02 22:10:26
I'm having some difficulty writing some Unit Tests to test a custom ModelBinder that I created. The ModelBinder I'm trying to Unit Test is the JsonDictionaryModelBinder that I posted here . The problem I'm having is getting the Mocking all setup using Moq. I keep getting Null Exceptions due to the HttpContextBase not being Mocked correctly. I think. Could someone help me figure out what I'm not doing correclty? Here's a sample of the Unit Test I'm trying to write that doesn't work: [TestMethod()] public void BindModelTest() { JsonDictionaryModelBinder target = new JsonDictionaryModelBinder();

How to test asp.net core built-in Ilogger

你说的曾经没有我的故事 提交于 2019-12-02 21:53:26
I want to verify some logs logged. I am using the asp.net core built-in ILogger, and inject it with the asp.net core built-in DI: private readonly ILogger<InvoiceApi> _logger; public InvoiceApi(ILogger<InvoiceApi> logger) { _logger = logger; } then I use it like: _logger.LogError("error)); I tried to mock it (with moq) as usual by: MockLogger = new Mock<ILogger<InvoiceApi>>(); and inject this in the service for test by: new InvoiceApi(MockLogger.Object); then tried verify: MockLogger.Verify(m => m.LogError(It.Is<string>(s => s.Contains("CreateInvoiceFailed")))); but it throw: Invalid verify on

Moq with Task await

こ雲淡風輕ζ 提交于 2019-12-02 21:39:15
Since I have converted my WCF methods to Async, my unit tests have failed, and I can't figure out the correct syntax to get them to work. Cllient proxy class public interface IClientProxy { Task DoSomething(CredentialDataList credentialData, string store); } service class public class CredentialSync : ICredentialSync { private ICredentialRepository _repository; private IClientProxy _client; public CredentialSync() { this._repository = new CredentialRepository(); this._client = new ClientProxy(); } public CredentialSync(ICredentialRepository repository, IClientProxy client) { this._repository =

How to do internal interfaces visible for Moq?

狂风中的少年 提交于 2019-12-02 20:05:44
I have 3 project in my C# solution. Signatures Structures Tests Signatures has public and internal interfaces. Also it has [assembly: InternalsVisibleTo("Structures")] [assembly: InternalsVisibleTo("Tests")] in AssemblyInfo.cs of. Structures has public and internal classes and [assembly: InternalsVisibleTo("Tests")] in AssemblyInfo.cs of. Tests has next source: <packages> <package id="Moq" version="4.2.1409.1722" targetFramework="net45" /> <package id="NUnit" version="2.6.4" targetFramework="net45" /> <package id="NUnitTestAdapter" version="1.2" targetFramework="net45" /> </packages> as NuGet