I have a repository interface, for example:

public interface IUserRepository { IEnumerable<User> ListUsers(); User GetUserById(int id); void AddUser(User user); void DeleteUser(User user); void EditUser(User user); } 

where User , for example, looks like this:

 public class User { public int Id { get; set; } public string Name { get; set; } public IEnumerable<Role> { get; set; } public string Email { get; set; } } 

just in case,

 public class Role { public int Id { get; set; } public string Name { get; set; } } 

What is the easiest way to make an imitation of this repository with fake data?

  • If you simply make a singleton inside which you define a List<User> , then this confuses me, because there will be differences in behavior from the real repository. If in the real repository two times GetUser(id) (with the same id ), then we get two instances of the object. And if in the singleton or static class we also get 2 times from List<> , then we get one instance of the object.

  • If you do a text file or xml , it seems like a long time. I would like to find the easiest option. But if this is the easiest option, then tell me, then I will answer what happened. With regard to SQLite , the more long fence, not worth it.

  • If you use Moki , it is embarrassing that I have almost no experience with them, and the fact that the data (for example, name, Email) will not look beautiful. It seems that in my situation it is easier to enter somehow the data for 5-10 users.

UPDATE: It is enough to save the state within one session of the program. After it is turned off, you can reset the added or modified User s

  • You for tests or for another? - andreycha
  • @andreycha, no, not for tests. I do the user interface. While the server part is not ready (day or three days), I made stubs for client services. Then, with the help of IoC, a submenu of their implementations that will access real server services. - Andrey K.
  • When imitated reports, I liked using NBuilder + Faker to generate random data. They can also be conveniently used for the initial filling of the sheet, if you use the answer Qwertiy "Static class". - Andrey K.

2 answers 2

A static class, but to give and keep in it always copies of objects, not the objects themselves. Naturally, I mean deep copies.

  • Great! I will try to make copies for this option. Performance doesn't concern me so much for the Fake Repository . Daws, maybe I will put later. - Andrey K.

I advise you to consider the Moq framework

I do not recommend using the fake repository class, because

  1. There are many interfaces in the application, fakes will also be not small, and when changing the interface (adding a new method, for example), you will need to remember about the need to implement changes with fakes

    In general, it threatens that no one wants to support fakes.

  2. Definitely want to (and in most cases just need) to write at least a dozen tests on a particular method, as a result - there is a problem in the intersection of the data. Data suitable for one test, not suitable for another

Moq (or equivalent) allows you to create your own unique data set for each test within each test.

  • Thank! I think, than to deal with a new framework for me, it's easier now to make fakes (in this particular situation of mine). Still, because I do a client, then there are no tests. Plugs - a temporary measure. But there is IoC . It does not seem to go on the move how to register such a card so that nothing but one line of registration will be changed in the future. But the answer to the topic and useful. - Andrey K.
  • By the way, how to make random data in moq ? Do they even look cute or something like Name01, Name02, Name03... ? Still, if the service methods are asynchronous, then you can use this framework? - Andrey K.