RhinoMocks - Pass Action<T> as parameter - c#-3.0

In RhinoMocks, there's Stub extension method, that takes Action<T>. For some reason this:
CurrentInvoice.Stub(i => i.TaxYear).Return(1);
works great, but this:
CurrentInvoice.Stub(new Action<Invoice>(i => i.TaxYear)).Return(1);
produces the compiler error:
Only assignment, call, increment, decrement, and new object expressions can be used as a statement
The intellisense for this method explicitly says that it expects Action<Invoice>, so I can't understand why the first works, but not the second.
The main relevance of this is that I'd like to be able to pass some of these configuration lambdas as parameters to a method, and I run into this same issue.
Thanks

Are you sure you're not accidentally using an overload for Stub which takes a Func<T, TResult> in the first line? I can't see why the first call would work otherwise.
Do you have a link to API documentation?

Related

Unable to properly stub a method with an argument instanciating a new Date

I'm working on a scala project and in my unit test I have to stub a method that takes as argument a Date(that is instanciated when calling the method), and I can't get to stub it porperly
However, I was able to find a turnaround using this post How to mock new Date() in java using Mockito
But I wonder if there is a better way to do this because I find that solution not very satisfying ...
here is the code I try to stub :
def foo(): Future[JsonObject] ={
[...]
for {
a <- b.bar(arg,atDate = Some(Date.from(Instant.now())))
} yield a
}
I tried to stub it like that
val b = mock[B]
when(b.bar(arg, _:Option[Date])).thenReturn(Future.successful(List()))
this doesn't parse,so I have to change it to :
val b = mock[B]
when(b.bar(arg, _:Option[Date])).thenReturn({ d:Date => Future.successful(List())})
and when I run it I have the following error :
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.
Maybe I'm missing something in the error message but I don't find it helpful.
Is there any way to tell the stub to take whatever value for the date?
Also why does it require to put a function in thenReturn part, although the return type of the function is Future[List[A]]?
thanks in advance
You have to use the any matcher, so your code looks like (here I'm assuming arg is a variable defined somewhere else in your test code)
when(b.bar(ArgumentMatchers.eq(arg), ArgumentMatchers.any())).thenReturn(Future.successful(List()))
Now that's a bit verbose, so if you upgrade to mockito-scala and use the idiomatic syntax it would look like
b.bar(arg, *) returns Future.successful(List())
if you have/use cats, you can even do
b.bar(arg, *) returnsF List()
for more info check the docs here

Assign return value to variable warning

In netbeans I use to call a method that returns a value, but I am directly calling it where I have to pass a parameter to the function i.e. Function(getValue()) where getValuue() returns String. So what I want to know is that what is more efficient way to call this method whether should I assign value to a string first and then pass that value to parameter, as netbeans suggests me and shows a warning there, or calling it directly is good? I know code runs fine but keeping in mind the efficiency or rules of coding should I consider this thing or not? Or how badly can it effect if I ignore it?
If you are only using that value once, then calling it directly where it is used as a parameter is fine.
In Java, this is fine:
MyClass myClass = new MyClass();
myFunction(myClass.getSomeValue());
Whereas in the following case:
MyClass myClass = new MyClass();
MyOtherClass myOtherClass = myClass.someLongComputation();
Int value = myFunction(myOtherClass);
anotherFunction(value, myOtherClass);
It may be better to have a local variable so that you avoid calling a long running calculation twice. However, for simple getValue()s, it really doesn't matter.

Eclipse JDT AST: how to find a calling method returns value of an instance variable?

I'm using Eclipse JDT AST to parse a given java source code. While parsing the code, when it hits a method invocation, I want to find out whether that particular method returns or sets a value of an instance variable (basically to find out whether the callee method is a getter/setter of the same class of caller method).
E.g.:
public void test(){
//when parsing the following line I want to check whether "getName"
//returns a value of an instance variable.
String x = getName();
//when parsing the following line I want to check whether "setName"
//sets the value of an instance variable.
setName("some-name");
}
I've used the AST plugin also find out a possible path which would help me to refer it from the API, but couldn't.
Please let me know whether this is possible and if so, which approach that would help me to get the required information.
Don't think that there is an api which tells you whether a method is a getter or a setter.
You will have to write code to do this. For a getter, you can probably simply check if the last statement in the method is a return statement which returns an instance variable.

Help with the Moles syntax for testing private method with generics

I've got a signature for a method that looks like this:
private IEnumerable BuildCustomerUpdatePlan(List localCacheChangedCustomers, List crmChangedCustomers){}
When I look at the moled object, the syntax (IntelliSense) of how to call the method and test itis absolutely confusing to me and every time I give it a shot, I get compilation errors. I've looked through the basic tutorials provided on MSFT's site, but I simply don't get how to test a private method using Moles or how to deal with the return type and multiple parameters.
Unfortuantely I've been unable to find other good HOWTO's or threads demonstrating a more complex sample than just working with a simple Add() method that spits out an INT and accepts an INT. :(
Tips?
In your testing project, first make sure you add a Moles assembly corresponding to the assembly-under-test. You'll also want to add an using statement of the assembly-under-test with .Moles appended so you can use the moled assembly.
Moles changes the names of the classes and methods to the form M[Original Class Name].[Original Method Name][typeof param1][typeof param2].... In your case a detour for that method could look like MClass.BuildCustomerUpdatePlanListList = (List x, List y) => { [code]};. That defines an anonymous method that takes two Lists as parameters and you'd put whatever code wanted in the function. Just make sure that you return an IEnumerable in that anonymous method.
Here's an example using Moles to detour Directory.GetFiles:
using System.IO.Moles;
[assembly: MoledType(typeof(System.IO.Directory))]
...
MDirectory.GetFilesStringString = (string x, string y) => new string[0];
Since the Directory class is a member of System.IO I use using System.IO.Moles; to specify that I want to use moled members of the assembly.
Moles requires you to specify the types Moled: [assembly: MoledType(typeof(System.IO.Directory))] does the job.
Finally, Directory.GetFiles takes two strings as parameters and returns a string array. To detour the method into returning the equivalent of no files found, the moled method just returns new string[0]. Curly braces are needed if you want multiple lines in the anonymous method and, if not detouring a void method, a return statement that matches the type the original method would return.

Dynamic Proxy using Scalas new Dynamic Type

Is it possible to create an AOP like interceptor using Scalas new Dynamic Type feature? For example: Would it be possible to create a generic stopwatch interceptor that could be mixed in with arbitrary types to profile my code? Or would I still have to use AspectJ?
I'm pretty sure Dynamic is only used when the object you're selecting on doesn't already have what you're selecting:
From the nightly scaladoc:
Instances x of this trait allow calls x.meth(args) for arbitrary method names meth and argument lists args. If a call is not natively supported by x, it is rewritten to x.invokeDynamic("meth", args)
Note that since the documentation was written, the method has been renamed applyDynamic.
No.
In order for a dynamic object to be supplied as a parameter, it'll need to have the expected type - which means inheriting from the class you want to proxy, or from the appropriate superclass / interface.
As soon as you do this, it'll have the relevant methods statically provided, so applyDynamic would never be considered.
I think your odds are bad. Scala will call applyDynamic only if there is no static match on the method call:
class Slow {
def doStuff = //slow stuff
}
var slow = new Slow with DynamicTimer
slow.doStuff
In the example above, scalac won't call applyDynamic because it statically resolved your call to doStuff. It will only fall through to applyDynamic if the method you are calling matches none of the names of methods on the type.