moq

Integrate Pex with MoQ

廉价感情. 提交于 2019-11-30 07:55:41
Can anyone point me to a resource that shows an example of how Pex can be used in conjunction with MoQ? Thanks Pex uses Moles for isolation (mocking). One can still use MoQ alongside Moles. It's actually preferred to use a framework like MoQ for stubbing and mocking when the code can allow for it, leaving only Moles for the stuff MoQ can't isolate (sealed classes, non-virtual methods, private members, etc). To backup my statements, Peli de Halleux (a member of the Pex and Moles project), mentions doing the same thing over on MSDN forums . I have been using Moles and you can code with both

Moq - mock.Raise should raise event in tested unit without having a Setup

北慕城南 提交于 2019-11-30 05:43:04
I have a presenter class, that attaches an event of the injected view. Now I would like to test the presenter reacting on correctly on the event. This is the view interface IView: public interface IView { event EventHandler MyEvent; void UpdateView(string test); } This ist the view implementing IView public partial class MyView : IView { public event EventHandler MyEvent; public MyView() { this.combo.SelectedIndexChanged += this.OnSelectedIndexChanged; } public void UpdateView(string test) { this.textBox.Text = test; } private OnSelectedIndexChanged(Object sender, EventArgs e) { if (this

The Purpose of Mocking

和自甴很熟 提交于 2019-11-30 05:42:42
What is the purpose of mocking? I have been following some ASP.NET MVC tutorials that use NUnit for testing and Moq for mocking. I am a little unclear about the mocking part of it though. The purpose of mocking is to isolate the class being tested from other classes. This is helpful when a class : connects to an external resource (FileSystem, DB, network ... ) is expensive to setup, or not yet available (hardware being developed) slows down the execution of the unit tests has a non-deterministic behavior has (or is) a user interface It also makes it easier to test for error conditions, as your

Moq and SqlConnection?

眉间皱痕 提交于 2019-11-30 05:01:58
I'm writing unit tests for one of our products and have been used Moq to successfully mock connections to Entity Framework. However, I've come across the following method: public static productValue findValues(string productName, string dbConnectionString) { try { SqlConnection conn = new SqlConnection(dbConnectionString); conn.Open(); //Do stuff } } Which accesses our database inside that method using a passed connection string. Is it possible to setup a mock DB using Moq and create a connection string which points to the mocked DB? I've trying doing something along the lines of var

AutoFixture: mock methods don't return a frozen instance

我们两清 提交于 2019-11-30 04:43:55
问题 I'm trying to write this simple test: var fixture = new Fixture().Customize(new AutoMoqCustomization()); var postProcessingAction = fixture.Freeze<Mock<IPostProcessingAction>>(); var postProcessor = fixture.Freeze<PostProcessor>(); postProcessor.Process("", ""); postProcessingAction.Verify(action => action.Do()); The Verify check fails. The code for postProcessor.Process is public void Process(string resultFilePath, string jobId) { IPostProcessingAction postProcessingAction =

Why does Autofixture w/ AutoMoqCustomization stop complaining about lack of parameterless constructor when class is sealed?

六月ゝ 毕业季﹏ 提交于 2019-11-30 04:20:51
问题 When I use Moq directly to mock IBuilderFactory and instantiate BuilderService myself in a unit test, I can get a passing test which verifies that the Create() method of IBuilderFactory is called exactly once. However, when I use Autofixture with AutoMoqCustomization , freezing a mock of IBuilderFactory and instantiating BuilderService with fixture.Create<BuilderService> , I get the following exception: System.ArgumentException: Can not instantiate proxy of class: OddBehaviorTests.CubeBuilder

MvcMailer unit tests: System.ArgumentNullException httpContext cannot be null

荒凉一梦 提交于 2019-11-30 04:12:31
问题 I cannot successfully run unit tests for MvcMailer using the visual studio test suite and Moq. I have copied the example from the wiki word for word but get the following exception every time: Test method MvcMailerTest.Tests.MailerTest.TestMethod1 threw exception: System.ArgumentNullException: Value cannot be null. Parameter name: httpContext Code is as follows: (Using the VS unit test framework - exact same error when using nUnit as in the example) //Arrange: Moq out the PopulateBody method

Moq, SetupGet, Mocking a property

风流意气都作罢 提交于 2019-11-30 04:08:58
I'm trying to mock a class, called UserInputEntity , which contains a property called ColumnNames : (it does contain other properties, I've just simplified it for the question) namespace CsvImporter.Entity { public interface IUserInputEntity { List<String> ColumnNames { get; set; } } public class UserInputEntity : IUserInputEntity { public UserInputEntity(List<String> columnNameInputs) { ColumnNames = columnNameInputs; } public List<String> ColumnNames { get; set; } } } I have a presenter class: namespace CsvImporter.UserInterface { public interface IMainPresenterHelper { //... } public class

How to mock a web service

和自甴很熟 提交于 2019-11-30 03:25:50
Do I have to rewrite my code to do this into an interface? Or is there an easier way? I am using Moq What I usually do is build a wrapper or an adapter around my web service and just mock that. for instance: public class ServiceAdapter: IServiceAdapter { public void CallSomeWebMethod() { var someService = new MyWebService(); someService.SomeWebMethod(); } } Then I just stub the service adapter. [Test] public void SomeMethod_Scenario_ExpectedResult() { var adapterMock = new Mock<IServiceAdapter>(); //do your test } been writing a couple of responses about unit testing and mocking lately. I

How to unit test with ILogger in ASP.NET Core

若如初见. 提交于 2019-11-30 02:33:07
This is my controller: public class BlogController : Controller { private IDAO<Blog> _blogDAO; private readonly ILogger<BlogController> _logger; public BlogController(ILogger<BlogController> logger, IDAO<Blog> blogDAO) { this._blogDAO = blogDAO; this._logger = logger; } public IActionResult Index() { var blogs = this._blogDAO.GetMany(); this._logger.LogInformation("Index page say hello", new object[0]); return View(blogs); } } As you can see I have 2 dependencies, a IDAO and a ILogger And this is my test class, I use xUnit to test and Moq to create mock and stub, I can mock DAO easy, but with