c# - Why do I get an ExpectationViolationException when using a Stub? -
this test using nunit 2.5 , rhino.mocks 3.5:
[testfixture] public class excelworksheetcolumntests { private const string _name = "f1"; private const int _index = 0; private mockrepository _repo; [setup] public void setup(){_repo = new mockrepository();} private excelworksheetcolumn createcolumnusingfixturefieldsbutwith(iexcelworksheet excelworksheet) { return new excelworksheetcolumn(_name, _index, excelworksheet); } [test] public void when_selectedshootcolumntype_is_changed_raises_selectedshootcolumntypechanged_event() { var stubworksheet = _repo.stub<iexcelworksheet>(); excelworksheetcolumn column = createcolumnusingfixturefieldsbutwith(stubworksheet); stubworksheet .stub(p => p.getselectedshootcolumntype(column)) .return(shootcolumntype.generic); _repo.replayall(); bool ithappened = false; column.selectedshootcolumntypechanged += (s, e) => { ithappened = true; }; column.selectedshootcolumntype = shootcolumntype.subjectid; assert.that(ithappened); } [teardown] public void teardown() { _repo.replayall(); _repo.verifyall(); } }
fails message:
rhino.mocks.exceptions.expectationviolationexception : iexcelworksheet.setselectedshootcolumntype( classphotolink.model.createsubjectlist.excelworksheetcolumn, subjectid);
expected #0, actual #1.
i thought stubs returning values or ignoring method calls. why trying verify call to
iexcelworksheet.setselectedshootcolumntype( iexcelworksheetcolumn column, shootcolumntype type)
?
in rhino mocks wiki says regards stub extension method "remember default rhino mocks ignore unexpected methods calls."
edits of test.
removing setup & teardown code, still fails:
[test] public void when_selectedshootcolumntype_is_changed_raises_selectedshootcolumntypechanged_event() { var repo = new mockrepository(); var stubworksheet = repo.stub<iexcelworksheet>(); excelworksheetcolumn column = createcolumnusingfixturefieldsbutwith(stubworksheet); stubworksheet .stub(p => p.getselectedshootcolumntype(column)) .return(shootcolumntype.generic); repo.replayall(); bool ithappened = false; column.selectedshootcolumntypechanged += (s, e) => { ithappened = true; }; column.selectedshootcolumntype = shootcolumntype.subjectid; assert.that(ithappened); }
but works hoped.
[test] public void when_selectedshootcolumntype_is_changed_raises_selectedshootcolumntypechanged_event() { var stubworksheet = mockrepository.generatestub<iexcelworksheet>(); excelworksheetcolumn column = createcolumnusingfixturefieldsbutwith(stubworksheet); stubworksheet .stub(p => p.getselectedshootcolumntype(column)) .return(shootcolumntype.generic); bool ithappened = false; column.selectedshootcolumntypechanged += (s, e) => { ithappened = true; }; column.selectedshootcolumntype = shootcolumntype.subjectid; assert.that(ithappened); }
so seems have repo.stub() versus mockrepository.generatestub();
using stub()
means do expect method call. unexpected calls calls methods on no expectation has been set.
update
i'm wondering if problem use of setup, teardown, , record/replay, unnecessary.
what happens if write test way:
[testfixture] public class excelworksheetcolumntests { private const string _name = "f1"; private const int _index = 0; private excelworksheetcolumn createcolumnusingfixturefieldsbutwith(iexcelworksheet excelworksheet) { return new excelworksheetcolumn(_name, _index, excelworksheet); } [test] public void when_selectedshootcolumntype_is_changed_raises_selectedshootcolumntypechanged_event() { var stubworksheet = mockrepository.stub<iexcelworksheet>(); excelworksheetcolumn column = createcolumnusingfixturefieldsbutwith(stubworksheet); stubworksheet .stub(p => p.getselectedshootcolumntype(column)) .return(shootcolumntype.generic); bool ithappened = false; column.selectedshootcolumntypechanged += (s, e) => { ithappened = true; }; column.selectedshootcolumntype = shootcolumntype.subjectid; assert.that(ithappened); } }
Comments
Post a Comment