Stubbing a generic method with different T - rhino-mocks-3.5

I have a Service Factory with a Generic method GetService. Based on what I pass for T it gets me a service for that particular class.
Now I have a method, which uses multiple of these services and I need to stub all of them. Ex: GetService<Coffee> , GetService<Tea>.
mockrepo.Stub(x => x.GetService<Coffee>().Expect(c => c.RetrieveList(coffeeCollection)).IgnoreArguments());
mockrepo.Stub(x => x.GetService<Tea>().Retrieve(1)).Return(testrefDataRefEle1);
In the above code, on the second stub I get error "Object reference not set to an instance of an object."

You can't do a recursive mock like that in Rhino.Mocks. You'll have to create a stub for whatever object has the Retrieve method and set that up to return testrefDataRefEle1. Then set up the stub of GetService<Tea> to return the other mocked object.

Related

How do i get the PsiMethodCallExpression object type?

How do I get the object type that a PsiMethodCallExpression refers to?
eg
Object x = new Object;
x.hashCode();
I can use the visitor and get the PsiMethodCallExpression, and I can get "hashCode", but how do I get "Object"?
As you can learn from the javadocs to the APIs in question, you can obtain the method being called by calling PsiMethodCallExpression.resolveMethod(), and after that you can obtain the class where the method is declared by calling PsiMethod.getContainingClass().
Edit - just added some code to make it obvious for all and sundry, use the "accept" method with the following:
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
super.visitCallExpression(expression);
PsiUtil.getMemberQualifiedName(expression.resolveMethod());
expression.resolveMethod().getContainingClass().getName();
expression.resolveMethod().getContainingClass().getQualifiedName();
}

Facing issue when trying to fake helper function

I am using Nunit and FakeItEasy for my MVC Controller functions.
My Test Code:
[Test]
public async Task Search_Success()
{
if (!isFakeInitialized)
InitializeFake();
url = "/N/UserSvc/v1/Types?skip=0&take=" + Constants.MaxSearchRowNumber;
Types= A.CollectionOfFake<Type>(3);
List<Type> found=new List<Type>(Types);
A.CallTo(() => nFake.GetDataAsync<IEnumerable<Type>>(fakeHttpSession, url)).Returns(Types);
var fakeHelper = A.Fake<helperFunctions>();
A.CallTo(() => FakeHelper.GetAvailableTypes(fakeHttpSession, found, true)).Returns(foundTypes);
//Act
var actionResult = await myController.SearchView();
var viewResult = actionResult as ViewResult;
//Assert
Assert.IsNotNull(viewResult);
Assert.AreEqual("Search", viewResult.ViewName);
}
I am getting error at
A.CallTo(() => nFakeHelper.GetAvailableTypes(fakeHttpSession, found, true)).Returns(foundTypes);
Error: cannot convert lambda expression to type object because it is not a delegate type.
Here is the helper function Code:
public List GetAvailableTypes(Session session,List allTypes,bool includeAllType)
{
List results = new List();
return results;
}
How can i overcome the error.
If nothing else, your A.CallTo should fail because GetAvailableLicenseTypes isn't virtual. I'm a little surprised at the error message, though.
I've tried to reproduce, but had to trim things down quite a bit and fill in missing code, and ended up getting
The current proxy generator can not intercept the specified method for the following reason:
- Non virtual methods can not be intercepted.
Are you able to include more information, starting with the full error, including stack trace?
var nmsFakeHelper = A.Fake<NMCHelperFunctions>();
A.CallTo(() => nmsFakeHelper.GetAvailableLicenseTypes(fakeHttpSession, foundLicense, true)).Returns(foundLicensTypes);
These two lines are your issue.
The first line declares nmsFakeHelper as a fake of concrete type NMCHelperFunctions.
The second line then defines the behaviour of the fake when it's GetAvailableLicenseTypes method is called.
In the background, FakeItEasy decides what type of fake it should use (mock, stub, etc.). If the type you are asking a fake of is concrete you get a stub. However, if you want to be able to define behaviour (define return values or validate that methods were called etc.) you need a mock instead of a stub.
To get FakeItEasy to decide to return a mock instead of a stub, you need to give it an interface type instead. This is because a mock needs to be able to intercept the method calls but in .NET, methods can only be intercepted if they are virtual calls. This happens when the type you are using is an interface, but cannot happen when the type you are using is a concrete type.
So to get around this problem, you should add an interface to the NMCHelperFunctions type that includes (at the very least) the GetAvailableLicenseTypes method (as well as any other methods you may).
This means that your first line will change to the following (assuming you name your interface iNMCHelperFunctions):
var nmsFakeHelper = A.Fake<iNMCHelperFunctions>();
Your second line would remain unchanged, and your test code should now work.
You may have to refactor your application code to use the interface type instead of the concrete type. There is some benefit from doing this because it allows your components to be swappable so it's easier to add or change behaviour in the future by writing a new class that adheres to the same interface and switching to that.

REALBasic/Xojo NilObjectException When Calling Class Method From Property

I'm writing a console application used for UDP-based chat.
I have a class called App whose Super is ConsoleApplication (the "main" class) and a UDPInterface class whose Super is EasyUDPSocket. In the App class, there is a property called UDP whose type is UDPInterface (UDP As UDPInterface). In the Run event handler, there is this code:
StdOut.WriteLine(UDP.GetIP)
UDPInterface's method GetIP consists of the following code (return type is String):
return LocalAddress
LocalAddress is an EasyUDPSocket method that simply retrieves the internal IP.
The problem I'm having is that when I call UDP.GetIP, the program returns a NilObjectException. I need to use the UDPInterface class as a property so its properties work the same across all the methods inside App.
Objects must be instantiated using the New keyword prior to use. An object which hasn't been instantiated will always be Nil, and using a Nil object will always raise a NilObjectException:
UDP = New UDPInterface
StdOut.WriteLine(UDP.GetIP)

Ninject Factory - "new" object being passed in instead of one called in factory method

I am using the Ninject Factory Extensions so that I can create objects that have services injected plus custom values
so:
public interface IGameOperationsFactory
{
ISpinEvaluator Create(GameArtifact game);
IReelSpinner CreateSpinner(GameArtifact game);
}
Then in module:
Bind<IGameOperationsFactory>().ToFactory().InSingletonScope();
Bind<ISpinEvaluator>().To<DefaultSpinEvaluatorImpl>();
Bind<IReelSpinner>().To<DefaultReelSpinnerImpl>();
The actual factory gets injected in a classes' constructor and is then used like:
_spinner = _factory.CreateSpinner(_artifact);
_spinEval = _factory.Create(_artifact);
Where _artifact is of type GameArtifact
Then in each of the implementation's constructors services plus the passed in objects are injected. The GameArtifact is successfully passed in the first constructor, and in the second one a "new" GameArtifact is passed in, i.e. not a null one but one with just default values as if the framework just called
new GameArtifact()
instead of passing in the already existing one!
The Constructor for the two objects is very similar, but the one that doesn't work looks like:
[Inject]
public DefaultReelSpinnerImpl(GameArtifact ga, IGameOperationsFactory factory, IRandomService serv)
{
_rand = serv;
_ra = ga.Reels;
_mainReels = factory.Create(_ra);
_winLine = ga.Config.WinLine;
}
Where the factory and serv are injected by Ninject and ga is SUPPOSED to be passed in via the factory.
Anyone have a clue why a new "fresh" object is passed in rather than the one I passed in??
I have rewritten you sample a little bit, and it seems to work fine. Could you provide more detailed code sample?
My implementation
I have changed verb Create to Get to match Ninject conventions
public interface IGameOperationsFactory
{
ISpinEvaluator GetSpinEvaluator(GameArtifact gameArtifact);
IReelSpinner GetReelSpinner(GameArtifact gameArtifact);
}
Ninject configuration
I have added named bindings to configure factory
Bind<ISpinEvaluator>()
.To<DefaultSpinEvaluatorImpl>()
.Named("SpinEvaluator");
Bind<IReelSpinner>()
.To<DefaultReelSpinnerImpl>()
.Named("ReelSpinner");
Bind<IGameOperationsFactory>()
.ToFactory();
ps: full sample with tests

Can a RhinoMock mock hold property values?

I have been stuck on this for a day or two, I have recently started using RhinoMocks (v3.5)and I have setup a test. A stub web service that returns a List collection and a class that calls it, and a mock object with a property i expect to be set as a result of the call to the web service. My code is like this:
[Test]
public void Call_WebService_list_populated()
{
IData stService = MockRepository.GenerateStub<IData>();
IDefault mockView = MockRepository.GenerateMock<IDefault>();
DefaultPresenter presenter = new DefaultPresenter(mockView);
presenter.StService = stService;
mockView.Stub(x => x.RequestingUser).Return("test");
List<string> testList = new List<string> { new string() };
stService.Stub(x => x.GetList("test")).Return(testList);
presenter.LoadList();
Assert.AreEqual(testList,mockView.List);
}
In the LoadList function it just assigns the List property of mockView the list returned from the webservice. I can get the test to work using this line:
mockView.AssertWasCalled(a => a.StoryListing = testList);
but i expected that the mock object would hold state and i could check the property directly. Am i doing something wrong or is this just the way you have to use rhino mocks ie: the mock object cant hold state as when i do the assert.areequal nunit says the mockView.List property is null.
By default, mocks don't handle get/set properties (not sure why. There's a way to change it but I can't remember offhand). You can generate your mockView as a stub (MockRepository.GenerateStub<IDefault>()) -- and stubs support property behavior.