Domanda

I am using NMOCK2 and I want my mock to return a List containing 1 element, . This is what I have written so far:

Expect.Once.On(mockDatabaseManager).
    Method("GetTablesNames").
    Will(Return.Value(new List<Result>())); 

Is it even possible to do such thing, and if it is, how should I do that?

Definition of Result:

public class Result
{
    private Dictionary<String, Object> _result = new Dictionary<string,object>();

    public string GetString(String columnName)
    {
        return _result[columnName].ToString();
    }

    public double GetDouble(String columnName)
    {
        return Double.Parse(_result[columnName].ToString());
    }

    public int GetInteger(String columnName)
    {
        return int.Parse(_result[columnName].ToString());
    }

    public void Put(String columnName, Object value)
    {
        _result.Add(columnName, value);
    }
}
È stato utile?

Soluzione

You are creating a new empty list by using this code:

new List<Result>()

If you want to create a list with a single element you can use a collection initializer:

new List<Result> { new Result() }

(The Result class wraps a dictionary. However, there seems to be no way to add entries to this dictionary so calling new Result() will create a pretty boring object but that may be just fine in a unit test.)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top