# 在插件的前端测试中，是否可以覆盖Site对象及其自己的fixture？

**URL:** <https://meta.discourse.org/t/is-it-possible-to-override-the-site-object-with-own-fixture-during-front-end-tests-of-a-plugin/249167>\
**Category:** Development\
**Created:** [2022年十二月16日 18:32 UTC](https://meta.discourse.org/t/is-it-possible-to-override-the-site-object-with-own-fixture-during-front-end-tests-of-a-plugin/249167 "2022-12-16T18:32:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)\
**Post date:** [2022年十二月16日 18:32 UTC](https://meta.discourse.org/t/is-it-possible-to-override-the-site-object-with-own-fixture-during-front-end-tests-of-a-plugin/249167/1 "2022-12-16T18:32:29Z")

</div>

我目前正在处理这个 PR：[FIX: presenting formatted location on User Card by merefield · Pull Request #73 · merefield/discourse-locations · GitHub](https://github.com/paviliondev/discourse-locations/pull/73)

我正试图用一个新的前端测试来覆盖这个更改。

但由于缺少一个必需的数据属性，测试无法运行。

我遇到的问题是如何使用此处保存的夹具成功覆盖 Site 中的数据：

> <https://github.com/merefield/discourse-locations/pull/73/files#diff-2832ca21c6e61a472acdc999c8da22e843c1ebec1a0b8d7a7ef85e4a8d5b5065>
>
> \* FIX: all places where geolocation is formatted (+ add covering & first FE test…)
> \* FIX: failure to build
> \* IMPROVE: add CI workflow
> \* FIX: specs (out of date with latest Rails)

其中包含 Locations 插件存在时 Site 的附加属性。

…并使其在整个测试中持久存在！

新测试在这里。

> <https://github.com/merefield/discourse-locations/pull/73/files#diff-4d360f60e91cae2f99c0368cf620bf82c41a22783b590c65b9899802a3ab0f44>
>
> \* FIX: all places where geolocation is formatted (+ add covering & first FE test…)
> \* FIX: failure to build
> \* IMPROVE: add CI workflow
> \* FIX: specs (out of date with latest Rails)

我似乎成功地用夹具加载了 Site。

在浏览器中运行 qunit 时，我会在这个调试点停止，并且可以确定它已正确加载：

```plaintext
  test("user card location - shows correct format", async function (assert) {
    const siteAttrs = {
      ...siteFixtures["site.json"].site,
    };
    PreloadStore.store("site", cloneJSON(siteAttrs));
    Site.resetCurrent();
    debugger;
    PreloadStore.get("site")

```

![image](https://global.discourse-cdn.com/meta/original/4X/0/c/6/0c638c4df0964d966c3085424b85df32ba0c1b43.png)

但当用户卡片被点击时，数据并非如你所料，而且关键是 `country_codes` 属性丢失了，所以测试在这里停滞不前：

> <https://github.com/merefield/discourse-locations/blob/2a686be1babb684901b5debfd7759fecf65b8338/assets/javascripts/discourse/widgets/user-location.js.es6#L30>

 ![image](https://global.discourse-cdn.com/meta/original/4X/1/7/3/1738c3bd79a375f94b898acd0c3e7520a0d7039a.png)

为什么我覆盖的 Site 数据会丢失？

---

<div class="post-metadata">

**Author:** ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)\
**Post date:** [2022年十二月20日 21:49 UTC](https://meta.discourse.org/t/is-it-possible-to-override-the-site-object-with-own-fixture-during-front-end-tests-of-a-plugin/249167/2 "2022-12-20T21:49:32Z")

</div>

好的，这真令人沮丧。

看起来 Site 对象的值是在这里设置的：

> <https://github.com/discourse/discourse/blob/93a4012ecb3af7306de0c51283d6dd2b2e90818c/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js#L132>

并且无法被覆盖？

我通过修改核心中的 fixture（在这里：[https://github.com/discourse/discourse/blob/93a4012ecb3af7306de0c51283d6dd2b2e90818c/app/assets/javascripts/discourse/tests/fixtures/site-fixtures.js）来检查了这一点，它能够识别我的修改。](https://github.com/discourse/discourse/blob/93a4012ecb3af7306de0c51283d6dd2b2e90818c/app/assets/javascripts/discourse/tests/fixtures/site-fixtures.js%EF%BC%89%E6%9D%A5%E6%A3%80%E6%9F%A5%E4%BA%86%E8%BF%99%E4%B8%80%E7%82%B9%EF%BC%8C%E5%AE%83%E8%83%BD%E5%A4%9F%E8%AF%86%E5%88%AB%E6%88%91%E7%9A%84%E4%BF%AE%E6%94%B9%E3%80%82)

但我不想要那样，我希望在我的插件测试中用存储在我插件中的 fixture 来覆盖它。

重申一下，我能够在测试 _代码_ 的范围内做到这一点，但一旦插件代码运行，范围似乎会拾取与核心测试助手加载的 fixture 相关的 site 对象……

🤔

---

<div class="post-metadata">

**Author:** ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)\
**Post date:** [2022年十二月28日 21:57 UTC](https://meta.discourse.org/t/is-it-possible-to-override-the-site-object-with-own-fixture-during-front-end-tests-of-a-plugin/249167/3 "2022-12-28T21:57:56Z")

</div>

我终于解决了这个问题。发现你可以像使用其他对象一样使用 `needs`：

线索在这里：

> <https://github.com/discourse/discourse/blob/953b823c5a134d247cade81fc4f2b79e4326ea31/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js#L359>

最终代码：

```plaintext
  needs.site(cloneJSON(siteFixtures["country_codes.json"]));

```

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [2023年一月27日 21:58 UTC](https://meta.discourse.org/t/is-it-possible-to-override-the-site-object-with-own-fixture-during-front-end-tests-of-a-plugin/249167/4 "2023-01-27T21:58:29Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
