In order to test a method that uses a stored procedure, a fake method has been created. This method is to return a list of ints.
Something like this ...
public virtual ObjectResult<Nullable<int>> available_IDs( ... )
{
List<int?> fakeAvailableIDList = new List<int?>();
fakeAvailableIDList.Add(1);
fakeAvailableIDList.Add(2);
fakeAvailableIDList.Add(3);
ObjectResult<Nullable<int>> result = fakeAvailableIDList.All(m => m > 0);
return result;
}
which fails with
Cannot implicitly convert type 'bool' to 'System.Data.Objects.ObjectResult<int?>'
tried (amoungst other things)
ObjectResult<Nullable<int>> result = fakeAvailableIDList.All(m => m > 0);
which gives
Cannot implicitly convert type 'System.Collections.Generic.List<int?>' to 'System.Data.Objects.ObjectResult<int?>'
how can I get a List<> into ObjectResult ?
The line
fakeAvailableIDList.All(m => m > 0);
returns a boolean because .All
returns True
or False
depending on whether or not all elements in the collection meet the specified condition.
So, a variable of type ObjectResult
can't be set to a variable of type Bool
.
The ObjectResult
and ObjectResult<T>
types have hidden constructors, which means you can't create instances at will. Also, the ObjectResult<T>
type is sealed, which means it can't be extended. So, you might be out of luck if you're looking for an easy way to create an ObjectResult
from an Enumerable
.
The easiest thing to do, I think, would be to change the type used by the method you're trying to test. So, if that method has the signature:
void Foo(ObjectResult<int?> result);
Change it to:
void Foo(IEnumerable<int?> result);
That will allow you to create a fake collection with which the method can be tested, and you'll still be able to pass the method an ObjectContext<int?>
type because ObjectContext<int?>
extends IEnumerable<int?>
.
I realize this has already been answered, but a solution I have come up with is to mock the specific Linq extension off of ObjectResult<T>
.
For example:
Mock.Get(PortalEntitiesMock).Setup(m => m.ExecuteSqlQuery(It.Is<String>(x => x.ToUpper().StartsWith("SELECT"))).FirstOrDefault()).Returns<string>(p=>p).Verifiable();
So, you can mock individual items on a ObjectResult<T>
object if not the actual ObjectResult<T>
.