简体   繁体   English

Rspec在控制器中测试分页

[英]Rspec testing pagination in the controller

I have a controller that gathers data to show on screen like this 我有一个收集数据的控制器,像这样在屏幕上显示

@searches.where(user: some_user)
         .between(date1, date2)
         .order("created_at desc")
         .limit(50)
         .offset(params[:pageOffset])

render json: { html: render_to_string(index.html.erb, locals: {searches: @searches}) }

In my controller test, I want to ensure that the limit and the offset work, but I can't do 在我的控制器测试中,我想确保限制和偏移量有效,但是我不能做

51.times { FactoryGirl.create(:search) }

Because a search needs to have a user as well as other properties that need to be set to test this properly. 因为搜索需要具有用户以及需要设置的其他属性才能正确测试此属性。

If I were to generate all the data required to get over 50 searches in the database, it would take way too long (I've tried, it's unacceptable) 如果我要生成在数据库中进行50多次搜索所需的所有数据,那将花费很长时间(我尝试过,这是不可接受的)

I need a better solution to test the limit and offset (aka my pagination) in a controller spec. 我需要一个更好的解决方案来测试控制器规格中的极限和偏移量(即我的分页)。 What is a better way that won't slow down the test suite? 有什么更好的方法不会减慢测试套件的速度?

I ended up making a design change that primarily helped the tests, but also opened up a future feature possibility. 最后,我进行了一次设计更改,该更改主要对测试有所帮助,但同时也为将来的功能开发提供了可能性。 My controller now looks like this: 我的控制器现在看起来像这样:

@searches.where(user: some_user)
     .between(date1, date2)
     .order("created_at desc")
     .limit(params[:page_limit])
     .offset(params[:page_offset])

render json: { html: render_to_string(index.html.erb, locals: {searches: @searches}) }

With my limit being set in the page like this: 在这样的页面中设置我的限制:

var pageLimit = pageLimit || 20;

I can set it or not set it, then when testing I can force it to be small to not overload my tests with factories. 我可以设置它,也可以不设置它,然后在测试时可以强制它变小,以免工厂过载测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM