亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 FluentAssertions.Primitives.ObjectAssertions

在 FluentAssertions.Primitives.ObjectAssertions

C#
肥皂起泡泡 2023-08-13 15:47:26
我已經開始FluentAssertions在 REST 端點的集成測試中使用庫。問題是我必須比較兩個實體,但排除它們的_id屬性。該屬性是從我的界面繼承的IEntity。public interface IEntity{    [BsonId]    ObjectId _id { get; set; }}例如Log類看起來像這樣[DataContract]public class Log : IEntity{    [BsonId]    public ObjectId _id { get; set; }    public string Message { get; set; }}在測試中我像這樣比較它們并且它有效retrieved.Should()         .BeEquivalentTo(expected, options => options.Excluding(member => member._id));但是,當我將此功能提取到擴展方法以供重用時,它不起作用。它不會忽略該_id成員。public static class ObjectAssertionExtensions{    public static void BeEquivalentToExcludingId<TExpectation>(this ObjectAssertions objectAssertion, TExpectation expectation) where TExpectation : IEntity    {        objectAssertion.BeEquivalentTo(expectation, options => options.Excluding(member => member._id));    }}當我將通用擴展方法更改為特定類型Log時,它就可以正常工作。我在這里準備了帶有示例的最小項目。有沒有辦法讓它工作,為什么它不能正常工作?我將嘗試檢查 github 存儲庫中的代碼FluentAssertions。謝謝。
查看完整描述

2 回答

?
一只萌萌小番薯

TA貢獻1795條經驗 獲得超7個贊

問題在于 Fluent Assertions 無法將_id泛型類型T_id具體類型關聯起來Log

#1077中報告了類似的問題,并通過#1087解決。截至撰寫本文時,我們尚未發布包含此修復程序的新版本。

2019-08-10 編輯:

Fluent Assertions 5.8.0 已發布并修復了該問題。



查看完整回答
反對 回復 2023-08-13
?
烙印99

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,但是很有效。


查看完整回答
反對 回復 2023-08-13
  • 2 回答
  • 0 關注
  • 179 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號