I want to Verify a class, but it does not have any public members. Here is the class:
public class SpreadsheetValuesList
{
private readonly List<SpreadsheetValue> _spreadsheetValues = new();
public SpreadsheetValuesList(string json)
{
_spreadsheetValues = JsonSerializer.Deserialize<List<SpreadsheetValue>>(json ?? "[]", new JsonSerializerOptions());
}
public void Add(SpreadsheetValue spreadsheetValue)
{
_spreadsheetValues.Add(spreadsheetValue);
}
public string ToJson()
{
return JsonSerializer.Serialize(_spreadsheetValues);
}
}
When I call verify on this object in a MSTest, I get this:
The Verify documentation does not seem to have anything about this, nor Stack Overflow. So I tried a few things:
- I (naively) made an override
ToString
method thinking that maybe Verify would call it and put it in the Test.received.txt file. It did not. - I searched the github repo thinking maybe there is an attribute I can use, but no.
- I tried TreatAsString, but this does not work either. If you read the fine print in the doc it says when the object is passed "directly" to verify. I am not doing that. I am only verifying it as a component of another object.
- If I expose the List as a public property
public List<SpreadsheetValue> Values { get; } = new();
then it appears in the received.txt file like this:
This is what I want, but I do not want to break the encapsulation of my class just so I can get a test to work. Is there a way to provide a method or attribute on a public method so Verify can output the data to the received/verified file?
This class is used as a component of other classes, so I am calling Verify on the larger class like this:
// Assert
await Verify(viewModel);