Unit testing mongoid、rspec和分配(:人)问题
我正试图为人事控制器编写我的第一个规范 如有必要,使用mongoid(2.0.1)、rspec(2.5.0)、mongoid rspec(1.4.2)和制造(0.9.5) (备注:模拟的组织模型继承自人员模型) 运行此规范时,我收到以下错误消息:Unit testing mongoid、rspec和分配(:人)问题,unit-testing,ruby-on-rails-3,rspec,mongoid,rspec2,Unit Testing,Ruby On Rails 3,Rspec,Mongoid,Rspec2,我正试图为人事控制器编写我的第一个规范 如有必要,使用mongoid(2.0.1)、rspec(2.5.0)、mongoid rspec(1.4.2)和制造(0.9.5) (备注:模拟的组织模型继承自人员模型) 运行此规范时,我收到以下错误消息: 1) PeopleController as logged in user GET 'index' assigns all people as @people Failure/Error: assigns(:people).sho
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)>'
1)PeopleController作为登录用户获取“索引”将所有人员分配为@people
失败/错误:分配(:人)。应eq(模拟人)
期望#
得到#
(使用==进行比较)
差异:
@@ -1,2 +1,6 @@
-#
+#
#./spec/controllers/people\u controller\u spec.rb:24:in'block(4层)in'
由于继承了_资源(1.2.2),我的控制器是干的,并且它应该在开发模式下工作
class PeopleController < InheritedResources::Base
actions :index
end
class PeopleController
你知道我做错了什么吗Mongoid::Criteria
object
提前感谢不幸的是,我遇到了同样的问题,我能想到的唯一解决办法是将此设置到控制器中:
def index
@people = Person.all.to_a
end
to_a
方法将Mongoid::Criteria
对象转换为数组。这种方法对我很有效,但我不知道如何使用继承的资源来解决这个问题。我也遇到了同样的问题,但捆绑后问题就消失了 就个人而言,我放弃了用存根测试继承的_资源索引操作,并测试返回的标准:
describe "GET 'index'" do
before do
get 'index'
end
it "returns http success" do
response.should be_success
end
it "should have news" do
assigns[:news].should be_a(Mongoid::Criteria)
assigns[:news].selector.should == {}
assigns[:news].klass.should == News
assigns[:news].options[:sort].should == [[:created_at, :desc]]
assigns[:news].options[:limit].should == 10
end
end
我最近遇到了类似的问题,我的控制器按预期返回,但我的rspec中的赋值(:topics)失败(返回的项目多于实际返回的项目) 这本质上是由于rspec分配无法加载通过继承资源获取的实际集合资源 解决方法我使用它通过使用logger.info强制加载分配(:主题):
class TopicsController < InheritedResources::Base
def index
# Needed for rspec assigns() to pass,
# since it doesn't evalue inherited_resources resource assignment
logger.info "Topics: #{@topics.inspect}" if Rails.env.test?
end
end
class TopicsController
希望这能有所帮助。您如何将
mongoid rspec
适应控制器测试?我在模型中使用了mongoid rspec
。。。您能告诉我如何将mongoid rspec
添加到控制器中吗?。非常感谢。
class TopicsController < InheritedResources::Base
def index
# Needed for rspec assigns() to pass,
# since it doesn't evalue inherited_resources resource assignment
logger.info "Topics: #{@topics.inspect}" if Rails.env.test?
end
end