在境界中测试平等 [英] Testing for equality in Realm

查看:98
本文介绍了在境界中测试平等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在单元测试中测试 Realm 对象之间的相等性。但是,我无法让对象返回 true 以获得相等。

I'm trying to test for equality among Realm objects in unit tests. However, I'm unable to get objects to return true for their equality.

根据 Realm docs here ,我应该可以这样做:

According to the Realm docs here, I should be able to do that:

let expectedUser = User()
expectedUser.email = "help@realm.io"
XCTAssertEqual(testRealm.objects(User.self).first!,
               expectedUser,
               "User was not properly updated from server.")

但是,我使用以下代码得到以下测试失败:

However, I get the following test failure with the following code:

领域模型

class Blurb: Object {
    dynamic var text = ""
}

测试

func testRealmEquality() {
    let a = Blurb()
    a.text = "asdf"
    let b = Blurb()
    b.text = "asdf"
    XCTAssertEqual(a, b)
}




XCTAssertEqual失败:(可选(Blurb {

text = asdf;

}))不等于(可选(Blurb {

text = asdf;

}))

XCTAssertEqual failed: ("Optional(Blurb {
text = asdf;
})") is not equal to ("Optional(Blurb {
text = asdf;
})")


推荐答案

来自Realm的Katsumi。 Realm对象的 Equatable 实现如下:

Katsumi from Realm here. Realm object's Equatable is implemented as follows:

BOOL RLMObjectBaseAreEqual(RLMObjectBase *o1, RLMObjectBase *o2) {
    // if not the correct types throw
    if ((o1 && ![o1 isKindOfClass:RLMObjectBase.class]) || (o2 && ![o2 isKindOfClass:RLMObjectBase.class])) {
        @throw RLMException(@"Can only compare objects of class RLMObjectBase");
    }
    // if identical object (or both are nil)
    if (o1 == o2) {
        return YES;
    }
    // if one is nil
    if (o1 == nil || o2 == nil) {
        return NO;
    }
    // if not in realm or differing realms
    if (o1->_realm == nil || o1->_realm != o2->_realm) {
        return NO;
    }
    // if either are detached
    if (!o1->_row.is_attached() || !o2->_row.is_attached()) {
        return NO;
    }
    // if table and index are the same
    return o1->_row.get_table() == o2->_row.get_table()
        && o1->_row.get_index() == o2->_row.get_index();
}

总之,a)如果两个对象都不受管理,它的工作方式与正常情况相同object的 Equatable 。 b)如果两个对象都被管理,如果它们是相同的表(类)和索引,它们是相等的。 c)如果一个是托管的,另一个是不受管理的,则不相等。

In summary, a) if both objects are unmanaged, it works same as normal object's Equatable. b) if both objects are managed, if they are the same table (class) and index, they are equal. c) If one is managed, another is unmanaged, ther are not equal.

托管表示对象已存储在Realm中。

"managed" means the object has stored in Realm.

您的代码中的 a b 不是等于。因为 a b 是非托管的(尚未存储在Realm中)并且它们是不同的对象。

So a and b in your code is not equal. Because a and b are unmanaged (have not stored in Realm) and they are different objects.

let a = Blurb()
a.text = "asdf"
let b = Blurb()
b.text = "asdf"
XCTAssertEqual(a.text, b.text)

此外,在测试相等性时,Realm不关心对象的值。领域只检查表和行索引(如b)所述。因为具有相同值的不同对象存储在数据库中是正常的。

Furthermore, when testing the equality, Realm doesn't care the values of the objects. Realm checks only a table and row index (as mentioned "b)"). Because different objects that has the same value are stored in the database is normal.

两个对象相等的示例如下所示:

An example that two objects are equal is like the following:

let a = Blurb()
a.text = "asdf"

let realm = try! Realm()
try! realm.write {
    realm.add(a)
}

let b = realm.objects(Blurb.self).first!
print(a == b) // true

这篇关于在境界中测试平等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆