gpt4 book ai didi

rspec - 如何在 RSPEC 中设置语言环境

转载 作者:行者123 更新时间:2023-12-04 12:17:37 28 4
gpt4 key购买 nike

我是 RSPEC 的新手。我编写了一个名为 result_spec.rb 的 RSPEC 代码,如下所示:

describe '#grouped_scores' do
subject { result.grouped_scores }

let(:result) { create(:result, user: user) }

its(:keys) { is_expected.to eq [1] }
its([1]) { is_expected.to be_within(0.001).of(6) }
end

然后当我在名为result.rb的模型中编写方法时,示例代码如下:
def grouped_scores
s = 0
if score > 10 && I18n.locale == :zh then
s = 2
end
return s
end

但是,当我在本地测试 RSPEC 时,我不断收到以下错误:
Failures:
1) Result#grouped_scores keys should eq [1]
Failure/Error: its(:keys) { is_expected.to eq [1] }

expected: [1]
got: []

(compared using ==)
# ./spec/models/result_spec.rb:39:in `block (3 levels) in <top (required)>'
2) Result#grouped_personality_scores [1] should be within 0.001 of 6
Failure/Error: its([1]) { is_expected.to be_within(0.001).of(6) }
expected 0 to be within 0.001 of 6
# ./spec/models/result_spec.rb:40:in `block (3 levels) in <top (required)>'

所以我想知道,是不是因为我没有将 I18n.locale 设置为“zh”,因此它没有得到值?如果是这样,如何在 RSPEC 中分配语言环境?或者还有什么我应该知道的来调试 RSPEC 中的错误?

请帮忙!谢谢!!

最佳答案

测试语言环境

# Assuming I have a LocalesController with check_for_locale action
describe LocalesController do

after(:each) do
I18n.locale = :en
end

it "should check if the locale is zh" do
get :check_for_locale, locale: :zh
expect(I18n.locale).to eq(:zh)
end

it "should check if the locale is set to default that is english" do
get :check_for_locale
expect(I18n.locale).to eq(:en)
end

end

locales_controller.rb
class LocalesController < ApplicationController

def check_for_locale

end

end

关于rspec - 如何在 RSPEC 中设置语言环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52040726/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com