Tag: 测试

针对每个用户角色使用RSpec重复测试描述

使用RSpec创建一些控制器测试,我发现自己为每个可能的用户角色重复了几个测试用例。 例如 describe “GET ‘index'” do context “for admin user” do login_user(“admin”) it “has the right title” do response.should have_selector(“title”, :content => “the title”) end end context “for regular user” do login_user(“user”) it “has the right title” do response.should have_selector(“title”, :content => “the title”) end end end 这是一个简单的例子,只是为了表明我的观点,但我有很多重复的测试…当然也有一些测试对每个上下文都是唯一的,但这并不重要。 有没有办法只编写一次测试,然后在不同的上下文中运行它们?

我可以使用RSpec测试已部署的站点吗?

我应该首先说,我不是很精通测试我的代码,直到最近我才完全没有这样做。 所以我有这个rails应用程序。 它基本上是一个API端点。 它需要请求并吐出JSON。 有很好的(我希望)测试覆盖率应该测试大多数function。 现在我们正在部署一个staging env,并希望让负载测试人员(独立部门)与之“玩”。 我想确定我们是否正确部署了它。 我是否可以针对此暂存部署运行RSpec的请求规范? 人们会做这种测试吗? 什么是最佳做法? 我该怎么读?

如何在水豚中单击此按钮

请帮我解决水豚这个问题 我在水豚中有一个这样的按钮: 我试过了 click_button “verify” 但它给出了错误: Failure/Error: find(‘#verify’).click NoMethodError: undefined method `node_name’ for nil:NilClass

Ruby on Rails用外键删除灯具

我用使用外键的灯具设置测试时遇到了麻烦! 如果有人能帮我理解这一点,我将不胜感激。 比方说,例如:user_type模型引用了:role模型,当执行测试时,测试数据库中的所有数据都被删除以再次重新插入,Rails首先从角色模型中删除数据,而不是首先从:user_type删除数据,然后从:role删除数据。 固定装置: #roles.yml technical: name: ‘Technical’ obs: ‘User Role for technical / maintenance users!’ #user_types.yml technic: role: technical name: ‘Technic’ is_admin: true 测试: #app/test/models/role_test.rb require ‘test_helper’ class RoleTest < ActiveSupport::TestCase fixtures :roles test 'save Role without name' do data = Role.new() data.valid? assert data.errors.added?(:name, :blank), 'Role saved without name!' end end #app/test/models/user_type_test.rb require […]

如何编写和inheritanceActionController :: TestCase的抽象子类

我有一堆Rails 3.1控制器,它们都有非常相似的测试要求。 我已经提取出公共代码(所有Test :: Unit样式),例如以下三个测试可以完全重用于所有这些: def create new_record = { field_to_update => new_value } create_params = { :commit => “Create”, :record => new_record } post :create, create_params end test “should_not_create_without_login” do assert_no_difference(count_code) do create; end assert_need_to_log_in end test “should_not_create_without_admin_login” do login_as_non_admin assert_no_difference(count_code) do create; end assert_needs_admin_login end test “should_create” do login_as_admin assert_difference(count_code) do create; end […]

为什么我的Rails Test :: Unit测试中我的fixture对象不可用?

根据此Rails指南 ,如果您创建一个fixture,它将在您的测试类中可用。 我在users.yml有这个夹具: stan: username: hashed_password: password_salt: email: 按照Rails指南,我试图像这样访问它: class SessionsControllerTest < ActionController::TestCase @user = users(:stan) # … end 我收到此错误: ./test/functional/sessions_controller_test.rb:5: undefined method `users’ for SessionsControllerTest:Class (NoMethodError) from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require’ from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require’ from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:155:in `require’

如何测试作为helper_method公开的Rails控制器方法?

它们似乎无法从ActionView :: TestCase访问

黄瓜和Rspec

任何人都可以建议我黄瓜和rspec教程(轨道3)的良好来源(简单的例子)??? 编辑: 其实我正在寻找免费的在线资源和良好的例子..

如何测试ActiveRecord中哪些validation失败?

我有这样的模型: class User (2..5) end 我想测试一下这个validation: it “should not allow too short name” do u = User.new(:name => “a”) u.valid? u.should have(1).error_on(:name) end 但是它没有测试在name设置了哪种错误。 我想知道,如果是too_short , too_long ,或者其他一些validation失败了。 我可以在errors数组中查找消息文本,如下所示: u.errors[:name].should include(I18n.t(“activerecord.errors.models.user.attributes.name.too_short”)) 但是当我在locale文件中设置activerecord.errors.messages.too_short而不是特定于模型的消息时,这将失败。 那么,是否可以检查出现了哪种错误?

测试rails app的综合指南

是否有关于测试rails应用程序的全面指南。 我正在寻找指导,告诉我应该测试什么,我应该如何测试它,特别是使用Rspec。 很多例子将不胜感激。