ruby on rails_在Ruby on Rails上进行测试[操作指南]

ruby on rails

在任何应用程序的软件开发生命周期中,测试阶段都是非常重要的一步。 它可以帮助软件开发人员在开发过程中更早地发现并修复错误。

这个简短的演示将使用几个Ruby宝石来帮助测试过程。 这些宝石是rspec-rails,shoulda-matchers和factory_bot_rails。 首先让我们看一下每个宝石的作用。

  • rspec-rails –这是对Rails 5+的框架测试
  • Shoulda-matchers:此gem提供了与RSpec和Mintiest兼容的单行代码,以测试通用的Rails功能,如果用手工编写,它们会更长,更复杂且容易出错。
  • factory_bot_rails:帮助创建具有现有数据库记录的ORM对象。

在编写任何代码之前,这里是完成此简短教程的要求。 您应该安装ruby 2.6.5或更高版本以及rails 5或更高版本。 现在让我们开始。

打开Gemfile,该文件位于项目的根目录中。 我们将在:development:test组中添加rspec-rails文件。

group :development, :testdo
    gem 'rspec-rails' , '~> 4.0.0'
end

运行bundle instal在端子L

然后运行rails generate rspec:install

这将生成运行rspec所需的文件。 我们将在:development:test组中安装第二个gem shoulda-matchers

group :development, :testdo
    gem 'shoulda-matchers’
end

然后运行bundle install

转到项目根目录中的spec文件夹。 打开rails_helper.rb文件。 将以下代码粘贴到文件的底部:

Shoulda::Matchers.configuredo |config|
     config.integrate do | with |
         with .test_framework :rspec
         with .library :rails
     end
end

最后,我们将在:development:test组中安装最后一个gem factory_bot_rails。

group :development, :testdo
    gem 'factory_bot_rails' , '~> 5.2'
end

然后运行bundle install 。 factory_bot_rails gem需要一些其他配置。 转到spec文件夹并在其中创建一个支持文件夹。 然后在support文件夹中创建一个factory_bot.rb文件。 将以下代码粘贴到文件中。

RSpec.configuredo |config|
    config.include FactoryBot::Syntax::Methods
end

在spec / rails_helper.rb文件中添加以下内容:

require './spec/support/factory_bot.rb'

我们将为此项目创建两个模型; 用户和帖子。 让我们从一个用户开始。 运行以下命令来创建用户。

rails generate model User name

您可以检查models文件夹中的user.rb文件。

我们将放置一个validates:name,presence:truehas_many:postshas_many:posts指示用户与帖子具有一对多关系,并且validate确保不能在列名上保存任何空值

现在,您的模型应如下所示:

class User < ApplicationRecord
    has_many : posts
    validates : name , presence : true
end

接下来,我们将创建一个帖子。 一个用户可以创建多个帖子,而一个帖子只能由一个用户创建。 这意味着该帖子在其表中具有属于用户的外键。 我们将创建帖子模型。

运行rails generate model Post content:text user:references

user:references在发布表上创建一个名为user_id的外键。 此user_id列是用户表的主键。 然后运行rails db:migrate

在app / models / post.rb中,您会注意到,有一个属地属代码行:user ,这意味着帖子只能由一个用户创建。 您可以在belongs_to:user下面添加validates:content,presence:true

您发布的模型应如下所示:

class Post < ApplicationRecord
    belongs_to : user
    validates : content , presence : true
end

导航到spec文件夹,您会发现已经创建了一个名为factory的文件夹。 在factory文件夹中,有一个user.rb。 factory文件夹中的user.rb的名称带有括号内的数据。 如果您没有注意到,这是数据库中属于用户的列。 如果转到db / schema.rb文件,您将在模式中看到该列。 同样的职位。 您在spec / factories / post.rb上看到的用户{nil}行是发布表中将其链接到用户表的外键。

现在我们已经建立了模型,让我们编写一些测试。 转到spec / models / user.rb文件并打开它。 我们将在模型中测试3种情况。 我们将测试用户与帖子的关系,测试不能在名称列中保存空值,以及是否可以将用户保存到数据库中。 Shoulda-matchers和factory_bot_rails宝石将使我们的工作容易得多。

让我们为用户模型编写测试。 您的用户模型应类似于:

require 'rails_helper'
    RSpec.describe User, type : :model do
        describe 'User Associations' do
            it { should have_many(:posts) }
       end
       describe 'Validation Tests for User' do
           let (:user) { build(:user) }
           it 'should validate user name' do
               user.name = nil
               expect(user.save).to eq( false )
           end
       end
       describe 'Create User' do
           let (:user) { build(:user) }
           it 'should save user' do
               expect(user.save).to eq( true )
           end
       end
 end

应有的have_many关键字来自shoulda-matchers gem。 他们帮助测试关系。 build(:user)使用factory_bot_rails创建一个用户实例。

在终端中输入rspec spec/models/user_spec.rb 。 3个测试用例均应通过。

让我们为Post模型处理测试用例。

在Post模型中编写以下代码:

require 'rails_helper'
RSpec.describe Post, type : :model do
    describe 'Post Associations' do
        it { should belong_to(:user) }
    end
    describe 'Validation Tes ts for Post' do
        let (:user) { create(:user) }
        let (:post) { attributes_for(:post) }
        it 'should validate post content' do
            post_test = user.posts.build(post)
            post_test.content = nil
            expect(post_test.save).to eq( false )
        end
    end
    describe 'Create Post' do
        let (:user) { create(:user) }
        let (:post) { attributes_for(:post) }
        it 'should save user' do
            post_test = user.posts.build(post)
            expect(post_test.save).to eq( true )
        end
    end
end

let(:post){attribute_for(:post)}允许我们以哈希形式传递post属性。 这使我们能够在user.posts.build(post )中传递帖子

由于帖子与用户之间存在多对一关系,因此我们使用post_test = user.posts.build(post) 。 这意味着要创建一个或多个帖子,必须有一个用户。

现在运行rspec spec/models/post_spec.rb 。 所有6个测试用例均应通过。 要运行所有测试用例,只需键入rspec。 就是这样! 这样,您就可以基于如何编写rspec测试用例的知识。

翻译自: https://hackernoon.com/testing-on-ruby-on-rails-a-how-to-guide-ox2u3y3i

ruby on rails

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值