我是 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
Run Code Online (Sandbox Code Playgroud)
然后当我在名为result.rb的模型中编写方法时,示例代码如下:
def grouped_scores
s = 0
if score > 10 && I18n.locale == :zh then
s = 2
end
return s
end
Run Code Online (Sandbox Code Playgroud)
但是,当我在本地测试 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)>'
Run Code Online (Sandbox Code Playgroud)
所以我想知道,是不是因为我没有将 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
Run Code Online (Sandbox Code Playgroud)
locales_controller.rb
class LocalesController < ApplicationController
def check_for_locale
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3612 次 |
| 最近记录: |