c# - Mocking OpenXML with Moq -
how should test following getworksheetpart
method:
public class exceldocument : iexceldocument { private readonly string _filepath; public exceldocument(string filepath) { _filepath = filepath; } public worksheetpart getworksheetpart(ispreadsheetdocument exceldoc, string sheetname) { sheet sheet = exceldoc.getsheet(sheetname); if (sheet == null) { throw new argumentexception( string.format("no sheet named {0} found in spreadsheet {1}", sheetname, _filepath), "sheetname"); } return exceldoc.getpartbyid(sheet.id); } }
where iexceldocument
, wrapper's spreadsheetdocumentwrapper
interfaces are:
public interface iexceldocument { worksheetpart getworksheetpart(ispreadsheetdocument exceldoc, string sheetname); } public interface ispreadsheetdocument { sheet getsheet(string name); worksheetpart getpartbyid(string id); }
here wrapper itself.
public class spreadsheetdocumentwrapper : ispreadsheetdocument { private spreadsheetdocument exceldoc; public spreadsheetdocumentwrapper(spreadsheetdocument exceldoc) { this.exceldoc = exceldoc; } public sheet getsheet(string sheetname) { return exceldoc.workbookpart.workbook.descendants<sheet>() .singleordefault(s => s.name == sheetname); } public worksheetpart getpartbyid(string id) { return (worksheetpart)exceldoc.workbookpart.getpartbyid(id); } }
finally, test tried implement getworksheetpart
. issue not sure how test functionality. idea worksheetpart
given sheet name , spreadsheet document.
public class test { [testclass()] public class excelupdatelogictests { [testmethod()] public void excel_getworkseetparttest() { mock<ispreadsheetdocument> mockspreadhseet = new mock<ispreadsheetdocument>(); sheet sheet = new sheet(); string id = "1"; sheet.name = "sheet"; sheet.id = id; mockspreadhseet.setup(doc => doc.getsheet("sheet")).returns(sheet); mockspreadhseet.setup(doc => doc.getpartbyid(id)).returns(????); mock<iexceldocument> mockexceldocument = new mock<iexceldocument>(); worksheetpart mockworkseet = mockexceldocument.object .getworksheetpart(mockspreadhseet.object, "sheet"); assert.istrue(mockworkseet.getidofpart(mockworkseet) == id); } } }
here general openxml structure tree spreadsheets:
spreadsheet | workbookpart / | \ workbook workbookstylespart worksheetpart | | | sheets stylesheet worksheet | / \ (refers sheetdata columns worksheetparts) | rows
since exceldocument
system under test there no need mock iexceldocument
. should mock/stub/fake dependencies needed test sut
now able test pass this...
public class test { [testclass()] public class excelupdatelogictests { [testmethod()] public void given_sheetname_exceldocument_should_getworkseetpart() { //arrange var stream = new memorystream();//avoid having use actual file on disk var spreadsheetdocument = spreadsheetdocument.create(stream, spreadsheetdocumenttype.workbook); // add workbookpart. workbookpart workbookpart = spreadsheetdocument.addworkbookpart(); workbookpart.workbook = new workbook(); // add worksheetpart. worksheetpart worksheetpart = workbookpart.addnewpart<worksheetpart>(); worksheetpart.worksheet = new worksheet(new sheetdata()); // add sheets list. sheets sheets = spreadsheetdocument.workbookpart.workbook.appendchild<sheets>(new sheets()); // append new worksheet , associate workbook. string expectedid = workbookpart.getidofpart(worksheetpart); string sheetname = "mysheet"; sheet sheet = new sheet() { id = expectedid, sheetid = 1, name = sheetname }; sheets.append(sheet); var wrapper = new spreadsheetdocumentwrapper(spreadsheetdocument); string fakefilepath = "path"; var sut = new exceldocument(fakefilepath); //act worksheetpart result = sut.getworksheetpart(wrapper, sheetname); //assert assert.isnotnull(result); var actualid = workbookpart.getidofpart(result); assert.areequal(expectedid, actualid); } } }
the process of doing raised questions current design.
if whole point of creating abstraction hide implementation details , reduce tight coupling on external framework make things easier mock , test having create actual spreadsheetdocument
, wrapping test seems redundant.
a lot of parts of framework difficult mock given internal generation. hide behind other abstractions, given not know enough end goal of system cannot advise design structure should taking.
Comments
Post a Comment