moq

Mocking OpenXML with Moq

江枫思渺然 提交于 2019-12-23 02:38:45
问题 How should I test the following GetWorksheetPart method: public class ExcelDocument : IExcelDocument { private readonly string _filePath; public ExcelDocument(string filePath) { _filePath = filePath; } public WorksheetPart GetWorksheetPart(ISpreadsheetDocument excelDoc, string sheetName) { Sheet sheet = excelDoc.GetSheet(sheetName); if (sheet == null) { throw new ArgumentException( String.Format("No sheet named {0} found in spreadsheet {1}", sheetName, _filePath), "sheetName"); } return

Mocking using MOQ and generic types

為{幸葍}努か 提交于 2019-12-23 02:32:54
问题 Method to unit test: GetUserInfo Following is the class containing the method: public class AccountService : IAccountService { IUnitOfWork _UnitOfWork; public AccountService(IUnitOfWork unitOfWork) { _UnitOfWork = unitOfWork; } public UserInfo GetUserInfo(string userName, string password) { var userInfo = new UserInfo(); userInfo.UserType = UserType.Invalid; // Statement of interest var portalUser = _UnitOfWork.Repository<DvaPortalUser>().Query().Filter(t => t.Email == userName && t.Password

Using Moq's VerifySet in VB.NET

旧时模样 提交于 2019-12-22 14:54:53
问题 I have a function that updates a user in the asp.net membership provider. <AcceptVerbs(HttpVerbs.Post)> Public Function EnableUser(ByVal id As String) As JsonResult Dim usr As StargatePortalUser = _membershipService.GetUser(id, Nothing) usr.IsApproved = True _membershipService.UpdateUser(usr) Dim response As New AjaxResponse(usr.UserName) Return Json(response) End Function I am trying to test this function to ensure the IsApproved property is set correctly <TestMethod()> Public Sub Service

Why this mock with Expression<Func<T,bool> is not matching?

为君一笑 提交于 2019-12-22 13:50:03
问题 i am new on mock and i am trying to do this mock example : Repository.cs public class Repository : IRepository { public List<Person> GetForExpression(Expression<Func<Person,bool>> expression ) { ... //get person from orm using expression } } PersonService.cs public class PersonService { private IRepository _repository; public PersonService(IRepository repository) { _repository = repository; } public List<Person> GetAllPersonsWith18More() { var result = _repository.GetForExpression(x => x.Age

moqing static method call to c# library class

做~自己de王妃 提交于 2019-12-22 11:17:21
问题 This seems like an easy enough issue but I can't seem to find the keywords to effect my searches. I'm trying to unit test by mocking out all objects within this method call. I am able to do so to all of my own creations except for this one: public void MyFunc(MyVarClass myVar) { Image picture; ... picture = Image.FromStream(new MemoryStream(myVar.ImageStream)); ... } FromStream is a static call from the Image class (part of c#). So how can I refactor my code to mock this out because I really

Mocking DbEntityEntry

删除回忆录丶 提交于 2019-12-22 10:50:04
问题 I am writing unit tests for my Generic Repository layer but i have some problems with DbEntityEntry . My Update method is like below. public virtual void Update(TEntity entityToUpdate) { var entity = dbSet.Find(context.Entry<ITrackedEntity>(entityToUpdate).Entity.Id); context.Entry(entity).CurrentValues.SetValues(entityToUpdate); } As you can see, context.Entry<TEntity>(TEntity entity) method is invoked by caller. So while unit tests this code throws null exception. I had tried to mock

Verify property is never set using Moq [duplicate]

别说谁变了你拦得住时间么 提交于 2019-12-22 10:34:50
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Moq - How to verify that a property value is set via the setter I would expect the following test to fail: public interface IObjectWithProperty { int Property { get; set; } } [TestMethod] public void Property_ShouldNotBeCalled() { var mock = new Mock<IObjectWithProperty>(); mock.Object.Property = 10; mock.Verify(x => x.Property, Times.Never()); } However, this test passes, even though Property is clearly

Mocking a private field

左心房为你撑大大i 提交于 2019-12-22 09:48:57
问题 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

Mocking UnitOfWork with Moq and EF 4.1

不问归期 提交于 2019-12-22 09:36:22
问题 I am working my way through the Contoso sample for some TDD practice and my tests for retrieving students are passing. My test for creating a new student fails (although the actual code works) as I believe the DBContext is not being mocked. What should I refactor in order to get this test to past? The test fails as so: Contoso.Tests.Controllers.StudentControllerTest.Create_HttpPost_Should_Save_New_Student: Expected: 9 But was: 8 Heres the concrete UnitOfWork public class UnitOfWork :

Mocking HttpContext.GetGlobalResourceObject in NUnit with Moq

◇◆丶佛笑我妖孽 提交于 2019-12-22 09:29:04
问题 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