1
我正在設置一些RhinoMock測試,但我無法弄清楚爲什麼我的期望失敗。爲什麼這個期望失敗?
這裏是類/接口我測試:
public class LogOn {
public virtual ILogOn View { get; set; }
public virtual IDataProvider DataProvider { get; set; }
public void SetUp(ILogOn view) {
this.View = view;
this.DataProvider = ... //using dependancy injection to do the data provider, so I want it faked in tests
}
public void SetUpEvents() {
this.View.Submit += new EventHandler(View_Submit);
}
void View_Submit(object sender, EventArgs e) {
if (this.DataProvider.LogOn(this.Username)) {
this.View.SubmitSuccess();
} else {
this.View.SubmitFailure("Username is incorrect");
}
}
}
public interface ILogOn {
string Username { get; set; }
event EventHandler Submit;
void SubmitSuccess();
void SubmitFailure(string message);
}
這裏是我的測試方法:
[TestMethod]
public void LogOnFailure() {
var dataProvider = MockRepository.CreateStub<DataProvider>();
var presenter = MockRepository.CreateMock<LogOn>();
var view = MockRepository.CreateMock<ILogOn>();
dataProvider.Expect(d => d.LogOn(null)).Return(true).Repeat.Any();
presenter.Expect(p => p.DataProvider).Return(dataProvider).Repeat.Any();
presenter.Expect(p => p.View).Return(view).Repeat.Any();
presenter.Expect(p => p.SetUpEvents()).CallOriginalMethod();
view.Expect(v => v.Username).Return("invalid").Repeat.Any();
view.Expect(v => v.SubmitFail(null)).Constraints(Is.Same("Username is incorrect"));
presenter.SetUp(view);
presenter.SetUpEvents();
view.Raise(v => v.Submit += null, null, EventArgs.Empty);
presenter.VerifyAllExpectations();
view.VerifyAllExpectations();
}
發生故障的期望是:
view.Expect(v => v.SubmitFail(null)).Constraints(Is.Same("Username is incorrect"));
(用view.VerifyAllExpectations
表示)
它說,它永遠不會執行的方法,但使用調試器時我可以逐步和LogOn.View
被訪問,並調用SubmitFailure
方法(這樣的說法),並正確地返回。
我看不出什麼是缺少的,因爲看代碼確實表明一切都在正確的時間和正確的值執行。
編輯:好了,我讓出這就是爲什麼我在嘲笑的LogOn
類的代碼,它有一個外部數據提供的扶養(這我磕碰,因爲我不關心它是如何工作的)。我的想法,我認爲我做得更清楚,但做得更糟!
那麼這對我來說太愚蠢了,我模擬LogOn的原因是因爲我不想讓SetUp正常運行。對數據提供者使用依賴注入(最初被忽略)。我不能沒有嘲笑登錄類(或因此我認爲) – 2009-07-30 03:41:58