English 中文(简体)
Mock testing with Repository pattern example
原标题:

I m refactoring existing class to support Products import from CSV file.

 

Requirements:

1) during import products are identified by special product number.

2) product has category attribute. If such category exist - use its id, if not - create it first.

3) if product with some number already exists in DB - do an update of other fields in another case - create a new product.

 

Goal:

Make a real unit-test(no DB interaction) verifiing that categories creation/reuse works fine.

 

Correct me if I m wrong:

1) I need to inject a list of existing product categories.

2) Loop through parsed products from CSV file and see whether category can be found in injected categories list or not.

3) What to return? Repository of aggregates(Should aggregate root be a product or product category?)

The problem is that we don t know what ID will new categories will obtain from DB.

Please give me some direction of how this problem can solved?


I m new to the Repository pattern(and persistence-ignorant domain concept) and I m using Mock testing in my daily coding.

最佳回答

As far as I can tell, you need to ask about the previous existence of both products and categories. This hints at two different Repositories: a ProductRepository and a CategoryRepository.

Injecting a list of existing categories is one possible approach, but you would also need to inject a list of existing products.

Another alternative would be to inject both Repositories and simply ask them whether the product or category already exists. If you need other functionality provided by these Repositories, this may be a better option, since you already have the required dependencies.

You might also want to consider doing both to keep closer to the Single Responsibility Principle. One collaborator could be a service that constructs a new Product instance based on the parsed data and the existing categories and products. Another would be responsible for retrieving the existing data. Yet another class would implement the CSV parser.

All types would implement interfaces, so that you might have collaborators such as these:

public class CsvParser : IParser
public class DataRetriever : IDataRetriever
public class ProductCreator : IProductCreator

Your overall class would then be one that takes those three interfaces as dependencies and orchestrates their interaction.

This will allow you to unit test each in isolation using mocks for each dependency.

问题回答

暂无回答




相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签