热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用Capybara选择任何项目-SelectanyitemfromselectusingCapybara

ImtryingtowriteatestforalittleRailsappImworkingon.TheUsersmodelhasahas_manythr

I'm trying to write a test for a little Rails app I'm working on. The Users model has a has_many through relationship (both ways) with other users and users have a has_many relationship with assignments. On the UI end of things, users can choose another user to attach new assignments to. I'd like to be able to test this. The problem right now, is that I can't figure out how to use Capybara to select from a list of items that I don't know the text to. I wrote a rake task to generate my test database randomly, and while I could look in the database and write my test that way, it would of course break the second I reset the database.

我正在尝试为我正在研究的一个小Rails应用程序编写测试。 Users模型与其他用户具有has_many through关系(两种方式),并且用户与assignments具有has_many关系。在UI端,用户可以选择其他用户来附加新的分配。我想能够测试一下。现在的问题是,我无法弄清楚如何使用Capybara从我不知道文本的项目列表中进行选择。我写了一个rake任务来随机生成我的测试数据库,虽然我可以查看数据库并以这种方式编写我的测试,但它当然会打破第二次重置数据库。

I'm only testing that the number of assignments in the test database changes by 1. I'm using Rspec with Capybara.

我只测试测试数据库中的分配数量变化1.我正在使用Rspec和Capybara。

2 个解决方案

#1


17  

Say the 元素的id为“foo”。然后以下应该工作。

option = first('#foo option').text
select option, from: 'foo'

#2


16  

You can find the option element, and then call select_option on it. For example, to select the third element in a drop down:

您可以找到option元素,然后在其上调用select_option。例如,要在下拉列表中选择第三个元素:

all('#foo option')[2].select_option

推荐阅读
author-avatar
昆哥2502852107
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有