gpt4 book ai didi

unit-testing - mongoid、rspec 和分配(:people) issue

转载 作者:行者123 更新时间:2023-12-04 04:49:35 26 4
gpt4 key购买 nike

我正在尝试为人员 Controller 编写我的第一个规范
如有必要,使用 mongoid (2.0.1)、rspec (2.5.0)、mongoid-rspec (1.4.2) 和制造 (0.9.5)。

(备注:模拟的 Organization 模型继承自 Person 模型)

describe PeopleController do
describe "as logged in user" do
before (:each) do
@user = Fabricate(:user)
sign_in @user
end

describe "GET 'index'" do
def mock_person(stubs={})
@mock_person ||= mock_model(Person, stubs).as_null_object
# @mock_person ||= Fabricate.build(:organization)  
end

it "should be successful" do
get :index
response.should be_success
end

it "assigns all people as @people" do
Person.stub(:all) { [mock_person] }
get :index
assigns(:people).should eq(mock_person)
end
end
end

我收到了 以下错误消息 当我运行这个规范时:
    1) PeopleController as logged in user GET 'index' assigns all people as @people
Failure/Error: assigns(:people).should eq(mock_person)

expected #<Person:0x811b8448 @name="Person_1001">
got #<Mongoid::Criteria
selector: {},
options: {},
class: Person,
embedded: false>


(compared using ==)

Diff:
@@ -1,2 +1,6 @@
-#<Person:0x811b8448 @name="Person_1001">
+#<Mongoid::Criteria
+ selector: {},
+ options: {},
+ class: Person,
+ embedded: false>
# ./spec/controllers/people_controller_spec.rb:24:in `block (4 levels) in <top (required)>'

由于inherited_resources(1.2.2),我的 Controller 是DRY的,并且可以在开发模式下正常工作。
class PeopleController < InheritedResources::Base
actions :index
end

任何想法我做错了什么 Mongoid::Criteria目的?

提前致谢

最佳答案

我有同样的问题,但捆绑后 mongoid-rspec问题消失了!

关于unit-testing - mongoid、rspec 和分配(:people) issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5949722/

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