gpt4 book ai didi

ruby-on-rails - Rspec:测试在应该通过的时候没有通过

转载 作者:数据小太阳 更新时间:2023-10-29 08:47:28 24 4
gpt4 key购买 nike

这里的第二个测试顺利通过,而第一个没有。不知道为什么。

require 'spec_helper'

RSpec.describe UserNotesController do

context 'GET #index' do

let(:note) { build(:user_note) }

specify 'populates an array of notes' do
expect(:user_notes).to eq [note]
end

specify 'renders the :index view' do
get :index
expect(response).to render_template :index
end
end
end

不知道为什么它没有通过,但我收到了这条消息。

(compared using ==)

Diff:
@@ -1,2 +1,2 @@
-[#<UserNote id: nil, user_id: 2, creator_id: nil, note: "Magnam quas fugit nihil.", created_at: nil, updated_at: nil, follow_up: nil>]
+:user_notes

我是 Rspec 的新手。有帮助吗?

最佳答案

要在 rspect 测试中访问实例变量 @foo,您可以使用 assigns(:foo)

引用它
expect(assigns(:user_notes)).to eq [note]

您还缺少对操作的调用。应该是……

get :index
expect(assigns(:user_notes)).to eq [note]

最后,let(:note) { build(:user_note) } 是惰性的,直到变量被引用才执行……这仅在 get :index 之后,并且build 在任何情况下都不会保存到数据库中,因此将其更改为 ...

let!(:note) { create(:user_note) }

关于ruby-on-rails - Rspec:测试在应该通过的时候没有通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23419772/

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