简体   繁体   English

这段代码可以用 rspec 进行轨道单元测试吗?

[英]This code can be rails unit test with rspec?

I'm studying rails and rspec.我正在研究 Rails 和 rspec。 And I made rspec unit test (request test) on rails application.我在 Rails 应用程序上进行了 rspec 单元测试(请求测试)。 But after searching on google, I'm wonder if my job is on right way.但是在谷歌上搜索之后,我想知道我的工作是否正确。 Can my code be a "Unit test by function(not a method, web site's feature ex)create, show, delete..) of rails application"?我的代码可以是“按功能进行单元测试(不是方法,web 站点的功能 ex)创建、显示、删除..)的 rails 应用程序”吗?

this is my code with request test.这是我的请求测试代码。

require 'rails_helper'

RSpec.describe 'Users', type: :request do
  let!(:users) { create_list(:user, 10) }
  let(:user_id) { users.first.id }
  let(:user) { create(:user) }

  def send_request_to_store_user(name, mailaddress)
    post '/users', params: {
      user: {
        name: users.first.name,
        mailaddress: users.first.mailaddress
      }
    }
  end

  def http_status_success_and_body_element_check(body_element)
    expect(response).to have_http_status(:success)
    expect(response.body).to include(body_element)
  end

  describe 'GET' do
    context 'Get /users test' do
      it 'test user list page' do
        get '/users'
        http_status_success_and_body_element_check('User List')
      end
    end

    context 'Get /users/create test' do
      it 'test user create page' do
        get '/users/create'
        http_status_success_and_body_element_check('create user')
      end
    end

    context 'Get /users/:id/edit' do
      it 'test user edit page' do
        get "/users/#{user_id}"
        http_status_success_and_body_element_check('edit user')
      end
    end

    context 'Get /users/:id' do
      it 'test user show page' do
        get "/users/#{user_id}"
        http_status_success_and_body_element_check('show user')
      end
    end
  end

  describe 'POST' do
    context 'test store new user' do
      it 'test create new user' do
        send_request_to_store_user(user.name, user.mailaddress)
        expect do
          create(:user)
        end.to change { User.count }.from(User.count).to(User.count + 1)
      end

      it 'test redirect after create' do
        send_request_to_store_user(user.name, user.mailaddress)
        expect(response).to have_http_status(302)
      end
    end
  end

  describe 'DELETE' do
    it 'test delete user' do
      expect do
        delete "/users/#{user_id}"
      end.to change { User.count }.from(User.count).to(User.count - 1)
      expect(response).to have_http_status(302)
    end
  end

  describe 'PUT' do
    context 'user update' do
      it 'test user information update' do
        old_name = users.first.name
        new_name = 'new_name'
        expect do
          put "/users/#{user_id}", params: {
            user: {
              name: new_name
            }
          }
        end.to change { users.first.reload.name }.from(old_name).to(new_name)
        expect(response).to have_http_status(:redirect)
      end
    end
  end
end

this is my code with test on model这是我在 model 上测试的代码

require 'rails_helper'

RSpec.describe User, type: :model do
  it 'user must have name and mailaddress' do
    user = create(:user)
    expect(user).to be_valid
    expect(user.name).not_to be_nil
    expect(user.mailaddress).not_to be_nil
  end

  it 'mailaddress must include @' do
    # user = FactoryBot.create(:user)

    # If rails_helper.rb has config.include FactoryBot::Syntax::Methods, 
    # Can use shortcut. Don't have to FactoryBot.create
    user = create(:user)
    # Test pass if email match with regexp
    expect(user.mailaddress).to match(/\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/)
  end
end

I don't think these tests are valuable (meaningful).我认为这些测试没有价值(有意义)。

Here's my reasoning:这是我的推理:

What are these tests telling you?这些测试告诉你什么? That the Rails router is working? Rails 路由器工作正常吗? That the controller is responding with the right action? controller 是否响应正确? Neither of these are your responsibility to test.这些都不是您测试的责任。 Rails has that covered. Rails 涵盖了这一点。

If you want to know "does the index page render?"如果您想知道“索引页面是否呈现?” and "can I CRUD a user?"和“我可以 CRUD 用户吗?” then write system tests with Capybara that simulate the whole flow.然后使用 Capybara 编写模拟整个流程的系统测试。 That way you are testing the real-world interaction with your whole system.这样,您就可以测试与整个系统的真实交互。

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

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