2 回答

TA貢獻1795條經驗 獲得超7個贊
問題在于 Fluent Assertions 無法將_id
泛型類型T
與_id
具體類型關聯起來Log
。
#1077中報告了類似的問題,并通過#1087解決。截至撰寫本文時,我們尚未發布包含此修復程序的新版本。
2019-08-10 編輯:
Fluent Assertions 5.8.0 已發布并修復了該問題。

TA貢獻1829條經驗 獲得超13個贊
首先,ObjectAssertionsExtensions改變是有意義的
public static void BeEquivalentToExcludingId<TExpectation>(this ObjectAssertions objectAssertion,
TExpectation expectation) where TExpectation : IEntity
到
public static void BeEquivalentToExcludingId(this ObjectAssertions objectAssertion,
IEntity expectation)
我還將每個斷言放入單獨的測試中以定位問題。
事情發生是因為只BeEquivalentToExcludingId期望IEntity擁有財產,卻得到額外的財產。這會讓一切都出錯。如果它不會損害您的架構,只需修改屬性即可解決問題。所以,唯一的改變是:_idLogMessageIEntitystring Message
public interface IEntity
{
[BsonId]
ObjectId _id { get; set; }
string Message { get; set; }
}
解決了問題。
更新:
考慮到您的評論,只需將要排除的成員設置為相同的值,調用BeEquivalentTo并設置實際值,如下所示:
public static void BeEquivalentToExcludingId(this ObjectAssertions objectAssertion, IEntity expectation)
{
var subj = (IEntity)objectAssertion.Subject;
var subjId = subj._id;
var expId = expectation._id;
subj._id = ObjectId.Empty;
expectation._id = ObjectId.Empty;
objectAssertion.BeEquivalentTo(expectation);
subj._id = subjId;
expectation._id = expId;
}
這很hacky,但是很有效。
- 2 回答
- 0 關注
- 179 瀏覽
添加回答
舉報