Verifying a method called with certain derived parameter - moq

Consider the following snippet;
public enum ReportType {Monthly,Quarterly}
public class BaseReport
{
public ReportType ReportType {get;set;}
}
public class MonthlyReport : BaseReport
{
public String month = "January"
public MonthlyReport() { ReportType = Monthly;}
}
public class Foo
{
public virtual void AddReport(BaseReport report);
}
[Test]
public void Test1()
{
var mock = new Mock<Foo>(){CallBase =true};
var report = new MonthlyReport();
mock.Object.AddReport(report);
}
Well I am trying to verify if the AddReport() is called with certain parameter here;
mock.Verify(x => x.AddReport(It.Is<MonthlyReport>(p => p.month == "January" &&
p.ReportType == ReportType.Monthly)));
As I feared, it doesn't work with a MonthlyReport parameter for Is<> even though it is derived from BaseReport. If I use Is,then I can't use p.month in the expression, and I am not that proficient with c# to know whether I can use if(p is MonthlyReport) in a lambda expression or more importantly, it would work as intended.
How can I approach this problem? Please note that the mock is partial, Although I can live with
Setup approach with callbacks if it is neatly solves my problem.
Any pointers would be greatly appreciated...

Related

Error in CreateInstance() while dynamically creating object of concrete type in Factory Pattern

I am actually new to design patterns and trying to implement factory pattern with .NET Core.
I tried to see couple of posts related to factory pattern and trying to implement it, I have added the concrete types in the config and reading it as dictionary in my code -
My Factory Interface -
public interface IEmpFactory
{
public BaseEmployee CreateEmployeeType<EmpType>()
where EmpType : BaseEmployee, new();
}
Implementation -
public class EmpFactoryImpl : IEmpFactory
{
public BaseEmployee CreateEmployeeType<EmpType>()
where EmpType: BaseEmployee, new()
{
return new EmpType();
}
}
Below are my services which are using the Factory as dependency -
public interface IEmpService
{
public string GetEmployeeBonus();
}
public class ContractEmpService : IEmpService
{
IEmpFactory _empFactory;
public ContractEmpService(IEmpFactory empFactory) =>
_empFactory = empFactory;
private BaseEmployee CreateMyEmployee() =>
_empFactory.CreateEmployeeType<ContractEmp>();
public string GetEmployeeBonus() =>
return CreateMyEmployee().GetBonus();
}
public class PermEmpService : IEmpService
{
private readonly IEmpFactory _empFactory;
public PermEmpService(IEmpFactory empFactory) =>
_empFactory = empFactory;
private BaseEmployee CreateMyEmployee() =>
_empFactory.CreateEmployeeType<PermEmp>();
public string GetEmployeeBonus() =>
CreateMyEmployee().GetBonus();
}
Added these concrete types in the config -
"ConfigurationProps": {
"EmpServices": {
"PermEmp": "SimpleFactoryWithoutSwitchCase.Service.PermEmpService",
"ContractEmp": "SimpleFactoryWithoutSwitchCase.Service.ContractEmpService"
}
}
Created the class to create a instance of the concrete type based on the type i.e, PermEmp or ContractEmp dynamically -
public class EmployeeTypeRouter : IEmployeeTypeRouter
{
private readonly ConfigurationProps _props;
public EmployeeTypeRouter(ConfigurationProps props)
{
_props = props;
}
public IEmpService GetInstance(string key)
{
string className = _props.EmpServices
.Where(k => k.Key.Equals(key)).FirstOrDefault().Value;
Type t = Type.GetType(className);
return (IEmpService)Activator.CreateInstance(t);
}
}
This is my calling method -
[HttpGet(Name = "GetEmployeeBonus")]
public string Get()
{
string type = "PermEmp";
IEmpService empService = _empRouter.GetInstance(type);
return empService.GetEmployeeBonus();
}
based on the type passed here i want to fetch the concrete type and call the method.
I am getting the error like this on CreateInstance method -
System.MissingMethodException: `Cannot dynamically create an instance of type 'SimpleFactoryWithoutSwitchCase.Service.PermEmpService'. Reason: No parameterless constructor defined.'
Which is very clear, but I don't want to create a parameterless constructor.
Since I am registering the dependencies in .NET Core, do I need to pass it again here? (which does not make sense for me)
Any help is really appreciated or if you feel I am doing something wrong please let me know.
Your EmployeeTypeRouter class tries to replicate the creation process that your DI Container can do more eloquently. So instead of calling Activator.CreateInstance, forward the resolution to the DI Container.
This means the following things:
Register all known IEmpService at startup.
Resolve the expected type from the IServiceProvider from inside the EmployeeTypeRouter.
In other words, change the startup code to the following:
var dictionary = props.EmpServices
.ToDictionary(p => p.Key, p => Type.GetType(p.Value));
foreach (string pair in dictionary)
{
services.AddTransient(pair.Value);
}
services.AddTransient<IEmployeeTypeRouter, EmployeeTypeRouter>();
services.AddTransient<Func<string, IEmpService>>(sp =>
key => (IEmpService)sp.GetRequiredService(dictionary[key]));
And change EmployeeTypeRouter to the following:
public class EmployeeTypeRouter : IEmployeeTypeRouter
{
private readonly Func<string, IEmpService> _factory;
public EmployeeTypeRouter(Func<string, IEmpService> factory)
{
_factory = factory;
}
public IEmpService GetInstance(string key) =>
_factory.Invoke(key);
}
In the previous code snippet, EmployeeTypeRouter makes use of the Func<string, IEmpService> delegate, which functions as factory. Under the covers the delegate calls back into the IServiceProvider.
There are of course several ways to skin a cat. You could also move some of the startup logic into EmployeeTypeRouter, or even remove the IEmployeeTypeRouter altogether and let application code depend directly on Func<string, IEmpService> delegate.

How do you use RegisterFactory?

I am having problems understanding how to use RegisterFactory. The code below works fine with the older InjectionFactory but I am having problems when I try to do the same thing with RegisterFactory.
In the sample code there is an uncommented section that uses RegisterFactory and a commented section that uses InjectionFactory. The InjectionFactory code works fine but the RegisterFactory throws an ResolutionFailedException.
Unity.ResolutionFailedException: 'The current type, ConsoleApp1.IFoo, is an interface and cannot be constructed. Are you missing a type mapping?
What am I doing incorrectly?
class Program
{
static void Main(string[] args)
{
var container = new UnityContainer();
container.EnableDebugDiagnostic();
container.RegisterType<IFoo, Foo1>("Foo1");
container.RegisterType<IFoo, Foo2>("Foo2");
container.RegisterType<MainViewModel>();
// This does not work
container.RegisterFactory<Func<string, IFoo>>((c, type, name) => c.Resolve<IFoo>(name));
// This works
//container.RegisterType<Func<string, IFoo>>(new InjectionFactory(
// ctx => new Func<string, IFoo>(name => container.Resolve<IFoo>(name))));
var vm = container.Resolve<MainViewModel>();
}
}
public class MainViewModel
{
public MainViewModel(Func<string, IFoo> fooFactory)
{
var foo1 = fooFactory.Invoke("Foo1");
var foo2 = fooFactory.Invoke("Foo2");
}
}
public interface IFoo
{
string Name { get; }
}
public class Foo1 : IFoo
{
public string Name { get; set; }
public Foo1()
{
Name = "Foo1";
}
}
public class Foo2 : IFoo
{
public string Name { get; set; }
public Foo2()
{
Name = "Foo2";
}
}
If you're registering with RegisterFactory, you tell Unity how to construct the instance. But Unity already knows how to construct Foo1 and Foo2, because you registered those already.
What you want is a factory for you to use, that's what RegisterType does, so this works.
Normally, such a factory would implement some IFooFactory, thus making the context more obvious. But as long as Func<string, IFoo> is registered only used once, it works fine, too, of course.

Change default session provider in ASP.NET

I want to change my session proviced to statically typed - I just hate typing strings because of many many errors I do.
What technology am I using? ASP.NET MVC via EXT.NET MVC
I was trying to do that using web.config but the problem is that after add session state to it visual is not going to compile my code because of that session should be using strings as keys.
I want to use session by enums such as :
public enum SessionEnum{Model}
public class Bar{
void foo(){
Session[SessionEnum.Model] = "blah";
}
}
I am aware that I can create wrapper converting enums to strings but it's not very satisfying solution for me.
public class StorageWrapper{
public object this[SessionEnum enum]{ get{return Session[enum.toString()]}; //+set
}
What I did was create static object for base class for all of my controllers and then I was able to use it across them but after closing and opening the page again I wasn't able to get values from it. I guess I should serialize them somehow but I have no idea how.
Is there any way to do that?
EDIT
My session now looks like this :
[Serializable]
public abstract class DataWrapper<T> : HttpSessionStateBase
{
Dictionary<T, object> Dictionary { get; set; } = new Dictionary<T, object>();
public object this[T a]
{
get
{
try
{
return Dictionary[a];
}
catch
{
return null;
}
}
set { Dictionary[a] = value; }
}
}
[Serializable]
public class SessionWrapper : DataWrapper<SessionNames>
{}
public enum SessionNames { Model, Login, LastOpenedFile }
It's very simple.
Create a UserSession object which does everything you want (holds your values as enum etc), instantiate it, then put it in the session.
var US = new UserSession();
US.stuff = somestuff;
Session["UserSess"] = US
Then you can just always use Session["UserSess"].stuff;
Mmmm, wouldn't you use static const string instead of an enum?
using System.Web;
public static class SessionEnum
{
public static const string Model = "_Session_Model";
public static const string Login = "_Session_Login";
public static const string LastOpenedFile = "_Session_LastOpenedFile ";
}
class test
{
void test()
{
Session[SessionEnum.Model] = "blah";
}
}

Value object design pattern in Flex

I am just looking design patterns used in Flex. Please tell me about value object design pattern and how is it implemented in Flex. Thank you.
A Value Object is really nothing more than a data object. It is OK to have some methods on a Value Object, in my opinion... but they exist for convenience and don't really add any behavior. For instance, here is an example of a VO:
[Bindable]
public class PersonVO {
public var firstName:String;
public var lastName:String;
public function PersonVO(firstName:String, lastName:String) {
this.firstName = firstName;
this.lastName = lastName;
}
public function clone():PersonVO {
return new PersonVO(firstName, lastName);
}
}
Note that this class is mutable by default which is why I added the [Bindable] tag. You are likely to want to use data binding with this class and you need [Bindable] (or some equivalent) to make that happen.
I actually prefer immutable value objects in many cases. Here is how you would implement a mutable VO:
public class PersonVO {
private var _firstName:String;
private var _lastName:String;
public function PersonVO(firstName:String, lastName:String) {
_firstName = firstName;
_lastName = lastName;
}
public function get firstName():String { return _firstName; }
public function get lastName():String { return _lastName; }
}
A VO is similar to a bean if you are from a Java background. I would code a VO as
public class UserVO
{
private var _name:String;
public function set name(value:String):void
{
_name = value;
}
public function get name():String
{
return _name;
}
}
Here we declare private variables and provide setter and getter methods to set the value for the members variables. Although it is not advised, you can perform range checking before assigning values inside the setter method. You can access the members as :
var userVo:UserVO = new UserVO();
userVo.name = "some name";
The following site is worth to have a look: http://www.flashmonkey.co.uk/using-value-objects-in-flash/

Moq: Return mock object with most implementation created

I want to mock my Repository object in such a way that it can still do actual DB retrieve operations. Only for Saving operations, I wanted to setup to return mock data since I don't want it to save into the DB.
How should I do it?
Thanks.
Maybe you should make your Save operation virtual and override it in a subclass which you use in your tests rather than using Moq?
First of all, your unit tests should never actually go out to the database (it is all right for integration tests, but that is a larger topic). What you want to do is pretty straightforward with Moq, though:
public class MyRepo
{
public virtual string Save(MyClass foo)
{
// perform save...
}
}
public class MyService
{
public MyRepo Repo { get; set; }
public string VerifyAndSave(MyClass foo)
{
// verify foo...
return new Repo.Save(foo);
}
}
public class MyClass()
{
public string SomeData { get; set; }
}
Notice the virtual modifiers on the methods--these are important for Moq to be able to stub them.
In your tests you could then do something like this:
[TestClass]
public class SomeTests
{
private Mock<MyRepo> MockRepo { get; set; }
private MyService Target { get; set; }
[TestInitialize]
public void Setup()
{
MockRepo = new Mock<MyRepo>();
Target = new MyService();
Target.Repo = MockRepo.Object;
}
[TestMethod]
public void MyTest()
{
const string expectedOutput = "SAVED";
MyClass exampleData = new MyClass();
MockRepo.Setup(x => x.Save(It.IsAny<MyClass>())).Returns(expectedOutput);
Target.VerifyAndSave(exampleData);
MockRepo.Verify(x => x.Save(It.IsAny<MyClass>()));
}
}
The chained calls of Setup and Returns in this case would guarantee that the calling method (i.e. VerifyAndSave) would see the value that you specified--"SAVED" in this case.
For more examples, take a look at the Moq quickstart docs.

Resources