moq

Overloaded return values in MOQ

纵然是瞬间 提交于 2019-12-12 04:13:52
问题 I'm working on understanding a bit more about Setup and unit testing with Moq. I've run into a slight problem though. What I want to do is something like this: view.Setup(x => x.GetReference("object1")).Returns(object1); view.Setup(x => x.GetReference("object2")).Returns(null); However, when I make my call this way, I never hit the block of code that would react to the Null statement. How am I supposed to set up my Setups so that they will behave in a specific way when they are called by a

How to Unit Test a GlassController Action which Uses Sitecore.Context.Item

梦想与她 提交于 2019-12-12 04:09:01
问题 I'm a sitecore developer and I want to create a sample sitecore helix unit testing project for testing out the logic you see in our "ArticleController" controller's Index() action method: public class ArticleController : GlassController { public override ActionResult Index() { // If a redirect has been configured for this Article, then redirect to new location. if (Sitecore.Context.Item.Fields[SitecoreFieldIds.WTW_REDIRECT_TO] != null && !string.IsNullOrEmpty(Sitecore.Context.Item.Fields

Assert that a method is called to bring an if condition under test

空扰寡人 提交于 2019-12-12 01:26:03
问题 Here is an example where I am testing the line if (true) . But although the condition is obviously true, Moq tells me the method was never called. public class test { public virtual void start() { if (true) called(); } public virtual void called() { } } [Test] public void QuickTest() { var mock = new Mock<test>(); mock.Object.start(); mock.Verify(t => t.start(), "this works"); mock.Verify(t => t.called(), "crash here: why not called?"); } How do I test that the method call to called() has

Testing Specific Method Calls with Parameters using Moq

≯℡__Kan透↙ 提交于 2019-12-12 00:13:26
问题 I'm trying to write a Unit Test with Moq to verify that a Registration was successful. My Test is as follows: [TestMethod()] public void RegisterTest() { //Arrange var MockRepo = new Mock<IDataRepo>() ; RegisterModel model = new RegisterModel { ConfirmPassword = "SamePassword", Email = "myemail@address.com", FirstName = "MyFirstName", LastName = "MyLastName", MiddleName = "MyMiddleName", Password = "SamePassword" }; MockRepo.Setup(ctx => ctx.Add(model)).Verifiable("Nothing was added to the

Using moq to verify a method is called on an mvc controller

无人久伴 提交于 2019-12-11 20:15:22
问题 I'm testing an MVC controller and want to test that a Save method is called within one of my repositories. The controller is an AccountController which is attempting to Register a user. My AccountRepository has two methods, Save and Register. Register is calling Save after some checks. I'm not sure if I should just be testing whether Register gets called and running a seperate unit test on my Repository to verify if Save is called as part of calling Register or whether I've set up Moq

Moqing HttpContext

99封情书 提交于 2019-12-11 19:53:26
问题 I have a unit test that tests an MVC controller's result. Unfortunately the controller uses a third party library that uses HttpContext.Request.IsLocal . I cannot refactor the third party library to make it use HttpContextBase . I need to mock this so that HttpContext.Request.IsLocal returns true. Any Ideas on how to achieve this? 回答1: If you are using premium or above version of vs2012, try looking at microsoft.fakes. it should cater to your situation. Some reading: http://msdn.microsoft.com

mocking a method with an anonymous type argument

只愿长相守 提交于 2019-12-11 18:43:52
问题 I have the following code: var connector = new Mock<IConector>(); connector .Setup(cn => cn.listar("FetchEstandar", new Estandar(), new {Id = 1})) .Returns(new List<Estandar>{ new Estandar {Id = 1} }); var entidad = connector.Object .listar("FetchEstandar", new Estandar(), new {Id = 1}); when I call listar on the connector Object, I get an "Expression Cannot Contain an Anonymouse Type" error. I've tried with rhino mocks and moq. Is there any way I can mock this method? am I doing something

How to Create a Unit Test for Adding Items in a Repository?

99封情书 提交于 2019-12-11 17:32:55
问题 I have an IUnitOfWork interface that encapsulates my custom repositories. My custom repositories in turn inherit from an IRepository interface. // The class that I am attempting to unit test // EmployeeBusiness.cs private readonly IUnitOfWork _unitOfWork; public EmployeeBusiness(IUnitOfWork unitOfWork) { _unitOfWork = unitOfWork; } public EmployeeDto AddEmployee(EmployeeDto employeeDto) { var employee = Mapper.Map<Employee>(employeeDto); if (employee == null) return null; _unitOfWork

How to verify the EventAggregator's unsubscribe method is called when disposing a ViewModel with Prism

萝らか妹 提交于 2019-12-11 16:14:57
问题 I'm struggling to write a test that confirms that I am correctly unsubscribing from an EventAggregator's message when it is closed. Anyone able to point out the (simple) answer?! Here is the code: public class ViewModel : BaseViewModel, IViewModel { private readonly IEventAggregator eventAggregator; private SubscriptionToken token; IssuerSelectedEvent issuerSelectedEvent; public ViewModel(IView view, IEventAggregator eventAggregator) { this.eventAggregator = eventAggregator; View = view;

What am I doing wrong this time with Moq?

ぐ巨炮叔叔 提交于 2019-12-11 14:41:02
问题 I am having problems with moq again and not sure what I did wrong this time. So I am going through the debugger step by step and I notice even though in my Mock I set ResetPassword to return "hey it does not seem to Here is part of my unit test: var membershipMock = new Mock<MembershipProvider>(); var user = new Mock<MembershipUser>(); user.SetupGet(x => x.Email).Returns("Email"); user.Setup(x => x.ResetPassword("test")).Returns("hey"); membershipMock.Setup(m => m.GetUser("chobo2", false))