How to write unit test for private method in c# using moq framework?

You can’t, at least not with Moq. But more importantly, you shouldn’t. First off, you don’t test methods, you test behaviours. Second, in order to test behaviours, you exercise a type’s public API and verify the outcomes of that exercise. Private methods are implementation details. You don’t want to verify how things get done, you … Read more

Verify a method is called or not in Unit Test

LicenseManagerService.Verify(m => m.LoadProductLicenses(),Times.Once); By calling the LicenseManagerService property, you’re creating a new mock object. Naturally, no invocations have ever been performed on this instance. You should change this property’s implementation to return the same instance every time it is called.

Unit test protected method in C# using Moq

Another way in Moq to call protected member is the following template: In your class, with protected member mark your function as virtual. For example: public class ClassProtected { public string CallingFunction(Customer customer) { var firstName = ProtectedFunction(customer.FirstName); var lastName = ProtectedFunction(customer.LastName); return string.Format(“{0}, {1}”, lastName, firstName); } protected virtual string ProtectedFunction(string value) { return … Read more

Proper way to Mock repository objects for unit tests using Moq and Unity

Unit tests should not use the container at all. Dependency Injection (DI) comes in two phases: Use DI patterns to inject dependencies into consumers. You don’t need a container to do that. At the application’s Composition Root, use a DI Container (or Poor Man’s DI) to wire all components together. How not to use any … Read more

C# how to mock Configuration.GetSection(“foo:bar”).Get()

I was able to solve it using ConfigurationBuilder. Hope this will help var appSettings = @”{“”AppSettings””:{ “”Key1″” : “”Value1″”, “”Key2″” : “”Value2″”, “”Key3″” : “”Value3″” }}”; var builder = new ConfigurationBuilder(); builder.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(appSettings))); var configuration= builder.Build();

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)