0

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);
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

1 Answers1

0

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.)

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
  • Thanks, that solves half of my problem. Unfortunately, I have to add an item to List, and Result does not have a constructor, and I can't change it. Is it possible to use Put method? – Marta Panuszewska Nov 18 '13 at 10:25
  • @MartaPanuszewska: All types always have constructors. With the code provided `Result` has a default constructor generated by the compiler and as I have pointed out you currently have no way to add elements to the dictionary. Either provide methods for that or create a suitable constructor. – Martin Liversage Nov 18 '13 at 10:27