NSubstitue mock IEnumerable Where() method - linq

I have the following code:
45 var listMock = Substitute.For<List<EntityTestObject>>();
46 listMock.Where(Arg.Any<Func<EntityTestObject, bool>>()).Returns(Something);
But I'm getting the following error:
System.ArgumentNullException : Value cannot be null.
Parameter name: predicate
at System.Linq.Enumerable.Where[TSource](IEnumerable'1 source, Func'2 predicate)
at line 46.
However, methods that does not take func<> arguments, such as Any(), do not fail.
My questions are:
How to avoid this mistake if it possible?
If not, how to mock Enumerable Extension methods?

NSubstitute can not mock extension methods like Enumerable.Where<T>. For the case of a List I would advise not mocking it at all. Use a real list and add the items you need for your test, so that the real Where(..) extension method will filter the items and provide the items required.

Related

Is it possible to to only stub a call with specific parameters with minitest?

I have multiple calls to ::Foo.bar. I would like to stub only the call that includes a very specific parameter.
Is it possible to do : ::Foo.stubs(:bar).with(1).returns(something) and still allow ::Foo.bar(2) to call the actual method?
You can use instance_of in the parameter, if the value will be an instance of the same class.
::Foo.stubs(:bar).with(instance_of(Integer)).returns(something)
Please refer the source code of with, a block can also be passed to match your requirements.

Enumerable invoke custom method

In PrototypeJS, the invoke method works nicely with an enumeration like,
$$('.mytags').invoke('setStyle',{fontWeight:bold})
I'm having trouble making this work against my own custom methods. What is the trick?
Let say I have:
function myMethod(element) {
element.insert('bingo');
}
If I try,
$$('.mytags').invoke('myMethod');
The code fails: TypeError: Cannot read property 'apply' of undefined
Any ideas?
myMethod() needs to be a method on the prototype of the items in the array.
for instance setStyle() is the method $('id').setStyle()
to accomplish what you are trying to do you need to define your method first and then add it to the prototype of the items you are trying to iterate over.
If the items are Elements then you should be able to use Element.addMethods()
http://api.prototypejs.org/dom/Element/addMethods/
to add the methods to the tags you want or add to all elements.

How to test a Reducer containing a Mutation

I'm attempting to use MRUnit, but none of the examples I've seen quite match what I'm trying to do.
My reducer outputs a key and mutation, but I can't seem to compare the mutation with what's expected. It shows the objects as being the same, but with an address of 0 and the following error:
junit.framework.AssertionFailedError: expected: <org.apache.accumulo.core.data.Mutation#0> but was <org.apache.accumulo.core.data.Mutation#0>
I'm using the reduceDriver.run() method, and attempting assertEquals on my expected mutation object with the actual. Is there anything I'm missing?
Thanks for any input.
Mutation does not have an appropriate equals() implementation. you're best bet is to probably compare the results of getUpdates() and getRow(). They return a List and byte[] respectively and those are easily comparable.
Mutation has an appropriate equals() method, at least it does in the 1.4.x line. However, that method calls a private serialize() method that modifies the data about to be checked.
I've gotten around this in the past by wrapping the Mutation in a new Mutation, which calls serialize under the hood:
assertEquals(expectedMutation, new Mutation(actualMutation));
You can extend Mutation and pass the new class to mrunit. Just override equals in the new class.

Why is there no Overload of Distinct() accepting a Comparison Delegate (or similar)

When using the query operator Distinct() the types in the queried sequence must either provide suitable overloads of GetHashCode() and Equals() or you have to pass an implementation of IEqualityComparer<T>.
My question: Why is there no overload of Distinct() accepting a Delegate instance (e.g. Comparison<T>)? - If it was existent a more lighweight lambda expression could be passed (more lightweight than an implementation of IEqualityComparer<T>). - Am I missing something here?
Because it uses GetHashCode().
You cannot make a delegate that gives hash codes.
It could take two delegates, but that would be confusing.
It would be better to ask why there isn't a DistinctBy() method that takes a projection.
otherwise you can try MoreLINQ and its method DistincBy

How Does Queryable.OfType Work?

Important The question is not "What does Queryable.OfType do, it's "how does the code I see there accomplish that?"
Reflecting on Queryable.OfType, I see (after some cleanup):
public static IQueryable<TResult> OfType<TResult>(this IQueryable source)
{
return (IQueryable<TResult>)source.Provider.CreateQuery(
Expression.Call(
null,
((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod(
new Type[] { typeof(TResult) }) ,
new Expression[] { source.Expression }));
}
So let me see if I've got this straight:
Use reflection to grab a reference to the current method (OfType).
Make a new method, which is exactly the same, by using MakeGenericMethod to change the type parameter of the current method to, er, exactly the same thing.
The argument to that new method will be not source but source.Expression. Which isn't an IQueryable, but we'll be passing the whole thing to Expression.Call, so that's OK.
Call Expression.Call, passing null as method (weird?) instance and the cloned method as its arguments.
Pass that result to CreateQuery and cast the result, which seems like the sanest part of the whole thing.
Now the effect of this method is to return an expression which tells the provider to omit returning any values where the type is not equal to TResult or one of its subtypes. But I can't see how the steps above actually accomplish this. It seems to be creating an expression representing a method which returns IQueryable<TResult>, and making the body of that method simply the entire source expression, without ever looking at the type. Is it simply expected that an IQueryable provider will just silently not return any records not of the selected type?
So are the steps above incorrect in some way, or am I just not seeing how they result in the behavior observed at runtime?
It's not passing in null as the method - it's passing it in as the "target expression", i.e. what it's calling the method on. This is null because OfType is a static method, so it doesn't need a target.
The point of calling MakeGenericMethod is that GetCurrentMethod() returns the open version, i.e. OfType<> instead of OfType<YourType>.
Queryable.OfType itself isn't meant to contain any of the logic for omitting returning any values. That's up to the LINQ provider. The point of Queryable.OfType is to build up the expression tree to include the call to OfType, so that when the LINQ provider eventually has to convert it into its native format (e.g. SQL) it knows that OfType was called.
This is how Queryable works in general - basically it lets the provider see the whole query expression as an expression tree. That's all it's meant to do - when the provider is asked to translate this into real code, that's where the magic happens.
Queryable couldn't possibly do the work itself - it has no idea what sort of data store the provider represents. How could it come up with the semantics of OfType without knowing whether the data store was SQL, LDAP or something else? I agree it takes a while to get your head round though :)

Resources