moq

Moq Unit of Work

风格不统一 提交于 2019-12-07 12:50:15
问题 I am new to unit testing and I want to create a test for my search feature. My service layer looks something like: public class EmployeeService: BaseService, IEmployeeService { public EmployeeService(IUnitOfWork unitOfWork) : base(unitOfWork) { _employeeRepo = unitOfWork.EmployeeRepository; } public IEnumerable<Employee> Search(Employee advancedSearch, int[] divisionIds, bool showInactive, int pageSize, int? page) { return _employeeRepo.Search(advancedSearch, divisionIds, showInactive,

How to test ServiceStack Service using Moq

安稳与你 提交于 2019-12-07 12:10:53
问题 I have a rest service that I have created with ServiceStack, using nHibernate as a way of getting the data from a SqlCe database. I've been trying to write some unit tests using nUnit and Moq - I have successfully mocked the nHibernate implementation to return null, invalid objects and so on - but my tests always throw a NullReferenceException when it calls the base class to set the HttpStatus etc. public List <ParameterDto> Get (ParameterAll parameterAll) { List <ParameterDto>

Mocking OpenXML with Moq

这一生的挚爱 提交于 2019-12-07 09:53:37
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 excelDoc.GetPartById(sheet.Id); } } Where IExcelDocument and wrapper's SpreadsheetDocumentWrapper interfaces

Moq Entity Frameworks ExecuteSQLCommand

可紊 提交于 2019-12-07 08:21:47
问题 I've read that when using moq you cannot mock a non-virtual function. I've also read that this should be possible now.. Is that true? If so, then I'd like to mock the following query: DatabaseContext.Database.ExecuteSqlCommand(updateQuery, newValue); I'm overriding the Context in my test as so DAL.Context.DatabaseContext = mockContext.Object; I've tried this setup, but it seems the query stills goes agains my regular db mockContext.Setup(c => c.Set<AppSalesAndResult>()).Returns(mockBudgetData

How do I mock HttpResponseBase.End()?

亡梦爱人 提交于 2019-12-07 07:08:48
问题 I'm using Moq to create a mock object of HttpResponseBase. I need to be able to test that HttpResponseBase.End() was called in my library. To do this, I specify some text before the call and some text after. Then I check that only the text before the call to End() is present in HttpResponseBase.Output. The problem is, I can't figure out how to mock HttpResponseBase.End() so that it stops processing, like it does in ASP.NET. public static HttpResponseBase CreateHttpResponseBase() { var mock =

c# mocking IFormFile CopyToAsync() method

坚强是说给别人听的谎言 提交于 2019-12-07 05:08:13
问题 I am working on a unit test for an async function that converts a list of IFormFile to a list of my own arbitrary database file classes. The method that converts the file data to a byte array is: internal async Task<List<File>> ConvertFormFilesToFiles(ICollection<IFormFile> formFiles) { var file = new File { InsertDateTime = DateTime.Now, LastChangeDateTime = DateTime.Now }; if (formFile.Length > 0) { using (var memoryStream = new MemoryStream()) { await formFile.CopyToAsync(memoryStream,

HttpResponseBase.Headers are empty when running test

╄→尐↘猪︶ㄣ 提交于 2019-12-07 05:04:23
问题 I'm using Moq to create a Mock<HttpResponseBase> to test an FileResult I'm creating for my MVC2 application. In the WriteFile(HttpResponseBase response) method of the FileResult , I have the following code at the end: // Write the final output with specific encoding. response.OutputStream.Write(output, 0, output.Length); response.AppendHeader("Content-Encoding", encoding); It will use utf-8 or gzip depending on the encoding from the request's Accept-Encoding header. So then in my test, I

Why does Moq setup/verify matcher fail when It.Is…() is called from anonymous function

▼魔方 西西 提交于 2019-12-07 04:11:57
问题 I ran into some strange behavior when attempting to simplify the creation of a rather complex expression tree for Setup/Verify matching with moq. Assume I am mocking the simple interface defined below public interface IService { int Send(int value); } The following code represents 5 tests. One test for each of the mockSender.Setup(...) . Can anyone explain why the tests marked as failing do fail? [Test] public void TestInlineSetup() { const int expected = 5; var mockSender = new Mock<IService

Proper way to verify parameters being passed to a Mock are set as expected

早过忘川 提交于 2019-12-07 02:54:34
问题 Is it acceptable to do asserts in your callbacks if you later verify that the methods were called? Is this the preferred way of making sure my mock is getting the expected parameters passed to it, or should I set a local variable in my callback and do the asserts on that instance? I have a situation where I have some logic in a Presenter class that derives values based on inputs and passes them to a Creator class. To test the logic in the Presenter class I want to verify that the proper

Dynamically calling Moq Setup() at runtime

戏子无情 提交于 2019-12-07 02:25:24
问题 I want to create a factory that will create commonly mocked objects for my unit tests. I've already managed to set up my tests so I can mock up a Linq2Sql DataContext and return an in memory table instead of hitting the database. I set it up like this: _contactsTable = new InMemoryTable<Contact>(new List<Contact>()); _contactEmailsTable = new InMemoryTable<ContactEmail>(new List<ContactEmail>()); // repeat this for each table in the ContactsDataContext var mockContext = new Mock