moq

Mocking VB.NET Methods With Moq

孤人 提交于 2019-12-06 00:15:54
问题 I am trying to unit test a controller action that uses the membership provider to update user details. I am using Moq which so far has been easy to use. The problem is I can't seem to get it to mock calls to methods that don't return anything. <TestMethod()> _ Public Sub Can_Update_User() ' Arrange _membershipService.Setup(Function(x) x.UpdateUser(It.IsAny(Of MembershipUser))) Dim controller As New UsersController(_membershipService.Object, _roleProvider.Object, _supportWorksService.Object,

Moq and reflection, passing dynamically generated expression tree / lambda to moq

为君一笑 提交于 2019-12-05 22:02:22
Is it possible to write code like the following. I'm trying to using Moq with objects that I'm reflecting on as part of a testing framework. The code below raises a "Unhandled expression type: 'Goto'" exception from Moq, which I guess is expecting something different. It kind of looks like it should work though! private void button1_Click(object sender, EventArgs e) { Ifoo = foo Foo(); // Create input parameter for lambda ParameterExpression value = Expression.Parameter(typeof(IFoo), "value"); // create return statement for lambda Expression setupProperty = Expression.Return(Expression.Label()

Moq Roles.AddUserToRole test

浪尽此生 提交于 2019-12-05 22:02:17
I am writing unit tests for a project in ASP.NET MVC 1.0 using Moq and MvcContrib TestHelper classes. I have run into a problem. When I come to Roles.AddUserToRole in my AccountController, I get a System.NotSupportedException. The Roles class is static and Moq cannot mock a static class. What can I do? You could use a pattern like DI (Dependency Injection). In your case, I would pass a RoleProvider to the AccountController, which would be the default RoleProvider by default, and a mock object in your tests. Something like: public class AccountController { private MembershipProvider _provider;

Mocking a private field

倾然丶 夕夏残阳落幕 提交于 2019-12-05 19:03:38
I know a similar question has been asked but I have not found a clear solution. I'm trying to mock a private field from a large class. The private field gets instantiated in some earlier method and I'm trying to unit test a latter method which references the field. So I have an earlier method in my class: public bool validateAll(ref DataEntry[] oEntries, string sMediaPlanId, ITemplateGenerator oTempGen) { ... // private field that I am trying to mock this._sMediaPlanObjective = (MPWrapper.Instance).getMediaPlanObjective(sMediaPlanId); ... } And I'm trying to Unit test a method that references

Mocking HttpContext.GetGlobalResourceObject in NUnit with Moq

匆匆过客 提交于 2019-12-05 18:44:38
I am trying to write a unit test which tests a method that uses HttpContext.GetGlobalResourceObject() and I would like to mock it using Moq. Let's say we're testing this method: public List<DctmGridColumn> GetDctmColumnsMandatory() { List<DctmGridColumn> metadataFields = new List<DctmGridColumn> { new DctmGridColumn(HttpContext.GetGlobalResourceObject("SharePoint.Service", "DctmGridColumn_DispName_r_object_id").ToString()), new DctmGridColumn(HttpContext.GetGlobalResourceObject("SharePoint.Service", "DctmGridColumn_DispName_object_name").ToString()), new DctmGridColumn(HttpContext

Does this test make proper use of AutoFixture and Moq?

半腔热情 提交于 2019-12-05 18:19:42
问题 Does this test make proper use of AutoFixture and Moq? Is it written as concisely as possible? The test fails, as expected, and passes after writing the correct implementation. [Fact] public void CustomerPropertyIsCorrect() { var fixture = new AutoMoqFixture(); var expected = fixture.Create<CardHolderCustomer>(); var builderMock = fixture .Freeze<Mock<ICustomerAdapter>>() .Setup(x => x.BuildCustomer()).Returns(expected); var sut = fixture.Create<CardHolderViewModel>(); var actual = sut

How to moq Entity Framework SaveChangesAsync?

妖精的绣舞 提交于 2019-12-05 17:58:11
问题 Mock<IDbContext> dbContext; [TestFixtureSetUp] public void SetupDbContext() { dbContext = new Mock<IDbContext>(); dbContext.Setup(c => c.SaveChanges()).Verifiable(); dbContext.Setup(c => c.SaveChangesAsync()).Verifiable(); dbContext.Setup(c => c.Customers.Add(It.IsAny<Customer>())) .Returns(It.IsAny<Customer>()).Verifiable(); } [Test] public async Task AddCustomerAsync() { //Arrange var repository = new EntityFrameworkRepository(dbContext.Object); var customer = new Customer() { FirstName =

How to Unit Test HtmlHelper similar to “using(Html.BeginForm()){ }”

£可爱£侵袭症+ 提交于 2019-12-05 17:52:18
Can somebody please suggest how I could write a Unit Test with Moq for following HtmlHelper method? public static HtmlTagBase GenerateTag<T>(this HtmlHelper htmlHelper , object elementData , object attributes) where T : HtmlTagBase { return (T)Activator.CreateInstance(typeof(T) , htmlHelper.ViewContext , elementData , attributes); } which you would use as follows (please note the using statement - this is causing me confusion): <%--Model is a type of ShareClass--%> <% using (Html.GenerateTag<DivTag>(Model)) { %> My Div <% } %> using this method, if you specify T as type DivTag , where

Testing With A Fake DbContext and Autofixture and Moq

自古美人都是妖i 提交于 2019-12-05 17:42:29
SO follow this example example and how make a fake DBContex For test my test using just this work fine [Test] public void CiudadIndex() { var ciudades = new FakeDbSet<Ciudad> { new Ciudad {CiudadId = 1, EmpresaId =1, Descripcion ="Santa Cruz", FechaProceso = DateTime.Now, MarcaBaja = null, UsuarioId = 1}, new Ciudad {CiudadId = 2, EmpresaId =1, Descripcion ="La Paz", FechaProceso = DateTime.Now, MarcaBaja = null, UsuarioId = 1}, new Ciudad {CiudadId = 3, EmpresaId =1, Descripcion ="Cochabamba", FechaProceso = DateTime.Now, MarcaBaja = null, UsuarioId = 1} }; //// Create mock unit of work var

Mocking out expression with NSubstitute

谁都会走 提交于 2019-12-05 16:46:42
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 "SecretAgentId" as I expect, so everything looks fine. My problem is that in our real production code we