how to fill ObjectResult with data from a list>

c# entity-framework testing

Question

A fake method has been developed in order to test a method that calls a stored process. A list of integers will be returned by this procedure.

anything similar to 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;
    }

it falters 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 results

Cannot implicitly convert type 'System.Collections.Generic.List<int?>' to 'System.Data.Objects.ObjectResult<int?>'

How can I enter a List into an ObjectResult?

1
3
1/19/2012 6:27:27 PM

Accepted Answer

The arrow

fakeAvailableIDList.All(m => m > 0);

gives a boolean result because.All returns True or False based on whether or not each member in the collection satisfies the requirement.

Therefore, a variable of typeObjectResult cannot be set to a type variableBool .

The ObjectResult and ObjectResult<T> Because types have secret constructors, you cannot arbitrary build instances of them. Further, theObjectResult<T> type cannot be expanded since it is sealed. Therefore, if you're seeking for a simple method to construct anObjectResult through anEnumerable .

I believe that changing the type that the function you're attempting to test uses would be the simplest course of action. As a result, if the method's signature:

void Foo(ObjectResult<int?> result);

Replace it with:

void Foo(IEnumerable<int?> result);

This will enable you to test the method using a fictitious collection and yet be able to pass it an evaluation.ObjectContext<int?> just becauseObjectContext<int?> extends IEnumerable<int?> .

3
1/19/2012 6:49:13 PM

Popular Answer

I'm aware that issue has previously been addressed, however I have come up with a workaround that involves mocking the relevant Linq extension.ObjectResult<T> .

For instance:

Mock.Get(PortalEntitiesMock).Setup(m => m.ExecuteSqlQuery(It.Is<String>(x => x.ToUpper().StartsWith("SELECT"))).FirstOrDefault()).Returns<string>(p=>p).Verifiable();

Consequently, you may mimic certain elements on an ObjectResult<T> if not the real itemObjectResult<T> .



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow