How to effectively test a parser with a fixed length of a flat file using MSpec?

I have this method signature: List<ITMData> Parse(string[] lines)

ITMData has 35 properties.

How would you effectively test such a parser?

Questions:

  • Should I download the whole file (can I use System.IO)?
  • Should I put a line from a file into a line constant?
  • Should I test one or more lines
  • Should I check every property of ITMData or check the whole object?
  • What about the name of my test?

EDIT

I replaced the method signature with ITMData Parse(string line).

Test code:

[Subject(typeof(ITMFileParser))]
public class When_parsing_from_index_59_to_79
{
    private const string Line = ".........";
    private static ITMFileParser _parser;
    private static ITMData _data;

    private Establish context = () => { _parser = new ITMFileParser(); };

    private Because of = () => { _data = _parser.Parse(Line); };

    private It should_get_fldName = () => _data.FldName.ShouldBeEqualIgnoringCase("HUMMELDUMM");
}

EDIT 2

, . -, , : 59 79, fldName. , . ?

:

[Subject(typeof(ITMFileParser))]
public class When_parsing_single_line_from_ITM_file
{
    const string Line = ""

    static ITMFileParser _parser;
    static ITMData _data;

    Establish context = () => { _parser = new ITMFileParser(); };

    private Because of = () => { _data = _parser.Parse(Line); };

    It should_get_fld??? = () => _data.Fld???.ShouldEqual(???);
    It should_get_fld??? = () => _data.Fld???.ShouldEqual(???);
    It should_get_fld??? = () => _data.Fld???.ShouldEqual(???);
    It should_get_fld??? = () => _data.Fld???.ShouldEqual(???);
    It should_get_fld??? = () => _data.Fld???.ShouldEqual(???);
    It should_get_fld??? = () => _data.Fld???.ShouldEqual(???);
    It should_get_fld??? = () => _data.Fld???.ShouldEqual(???);
    ...

}
+5
4

, , :

: , " " " ", . , , . , ; -/

  • . @mquander TextReader IEnumerable . , .
  • , ResourceManager assembly.GetManifestResource(). (- TextReader TextResource.Load( "NAME_OF_SOME_RESOURCE" )).
  • MSpec: . , , (It) . , . imho , , , , , . MSpec. , (, , , ). , , , , , , . , , frackin , , - .
  • : List <T> , List <T> . : ", , ", , , , . IEnumerable <T> ( ICollection <T> )
+2

( System.IO)?

, unit test - . , , , unit test. .

, , , , .

?

, , . , , , . ( , ).

?

, , -- - , , ITMData. .

, ( ). , , , , .

, -- , , - ParseSingleLine. Parse , ParseSingleLine. Parse, ParseSingleLine.

+4

, . . Pex .

+1

:

ITMData ?

, , .

?

, . , unit test, - . :

public void Check_All_Properties_Parsed_Correctly(){.....}

public void Exception_Thrown_If_Lines_Is_Null(){.....}

public void Exception_Thrown_If_Lines_Is_Wrong_Length(){.....}

, , , "" . , , , , . ! MSDN.

, , , . Test Driven Development, TDD, . , Kent Beck Test Driven Development By Example, - Microsoft.NET. .

EDIT:

?

-, . , :

, .

? , 2 , :

  • . CheckPropertyX, CheckPropertyY .. , , . , , . 2:
  • , . , , , . , , , Assert, , . :

Assert.AreEqual("test1", myObject.PropertyX, "Property X was incorrectly parsed"); Assert.AreEqual("test2", myObject.PropertyY, "Property Y was incorrectly parsed");

When one of them does not work, you will find out which line failed. When you correct the corresponding error and re-run your tests, you will see if any other properties have worked. This is usually the approach that most people take because creating a class or even a method for each property leads to too much code and too much work to update.

0
source

All Articles