首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RSpec没有在factory_bot对象中看到更新的属性

RSpec没有在factory_bot对象中看到更新的属性
EN

Stack Overflow用户
提问于 2019-04-24 10:27:03
回答 1查看 75关注 0票数 0

我有一个名为SiegeReport的report对象,它进行一些计算并返回整数对象。当建筑物中没有战士时,siege_ability就等于0。代码本身在这里并不重要,因为它在控制台和应用程序中工作得很好。factory_bot创建的工厂在所有其他示例中都正常工作。我只是在测试该方法时遇到了问题:

代码语言:javascript
复制
require 'rails_helper'

RSpec.describe Reports::SiegeReport do
  subject(:siege_report) { Reports::SiegeReport.new(building:    building).call }

  let(:building) { create(:building, granary: 100) }
  let(:clan) { create(:clan) }

    context 'with 1 infantry' do
      let(:warrior) { create(:warrior, clan_id: clan.id, building_id: building.id) }

      it 'returns 9' do
        expect(siege_report).to eq(9)
      end
    end
  end

RSpec返回:

代码语言:javascript
复制
Reports::SiegeReport siege ability with 1 infantry returns 9
      Failure/Error: expect(siege_report).to eq(9)

        expected: 9
            got: 0

        (compared using ==)

我用prywarrior对象检查了它是否有效,甚至返回战士,但是在属性中战士的数量仍然是0。当我在控制台中手动输入它时,同样的例子也能起作用。在测试之前,如何让RSpec更新构建属性?

EN

回答 1

Stack Overflow用户

发布于 2019-04-24 10:36:55

好的,我找到了解决方案,但实际上并不优雅:

代码语言:javascript
复制
it 'returns 9' do
  warrior.save
  expect(siege_report).to eq(9)
end

这是可行的,但在我看来,这不是正确的测试方式。不过,我还是找不到更好的解决办法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55827862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档