使用rspec和capybara进行无法计算的模型测试

时间:2016-11-02 18:03:19

标签: ruby-on-rails rspec

我正在使用rails 5.0.0.1为我独自向人们提供的某些设备构建一个小型库存类型系统。我的一个模型是命名设备,当然是一个不可数模型(单数和复数设备是相同的)。当我尝试为此模型编写功能测试时,当我尝试访问索引页时,我得到两个可能的错误之一。如果我使用"请访问equipment_url"我被告知没有路线可以显示"显示"因为没有身份证明。如果我想参加演出,这是一个正确的错误,但我不知道。如果我尝试"请访问equipment_index_url"我被告知没有方法访问。

Routes
                Prefix Verb   URI Pattern                             Controller#Action
                  root GET    /                                       inventory#index
      delete_equipment GET    /equipment/:id/delete(.:format)         equipment#delete
                       DELETE /equipment/:id/delete(.:format)         equipment#destroy
       equipment_index GET    /equipment(.:format)                    equipment#index
                       POST   /equipment(.:format)                    equipment#create
         new_equipment GET    /equipment/new(.:format)                equipment#new
        edit_equipment GET    /equipment/:id/edit(.:format)           equipment#edit
             equipment GET    /equipment/:id(.:format)                equipment#show
                       PATCH  /equipment/:id(.:format)                equipment#update
                       PUT    /equipment/:id(.:format)                equipment#update
                       DELETE /equipment/:id(.:format)                equipment#destroy
      delete_specialty GET    /specialties/:id/delete(.:format)       specialties#delete
                       DELETE /specialties/:id/delete(.:format)       specialties#destroy
           specialties GET    /specialties(.:format)                  specialties#index
                       POST   /specialties(.:format)                  specialties#create
         new_specialty GET    /specialties/new(.:format)              specialties#new
        edit_specialty GET    /specialties/:id/edit(.:format)         specialties#edit
             specialty GET    /specialties/:id(.:format)              specialties#show
                       PATCH  /specialties/:id(.:format)              specialties#update
                       PUT    /specialties/:id(.:format)              specialties#update
                       DELETE /specialties/:id(.:format)              specialties#destroy
          delete_event GET    /events/:id/delete(.:format)            events#delete
                       DELETE /events/:id/delete(.:format)            events#destroy
                events GET    /events(.:format)                       events#index
                       POST   /events(.:format)                       events#create
             new_event GET    /events/new(.:format)                   events#new
            edit_event GET    /events/:id/edit(.:format)              events#edit
                 event GET    /events/:id(.:format)                   events#show
                       PATCH  /events/:id(.:format)                   events#update
                       PUT    /events/:id(.:format)                   events#update
                       DELETE /events/:id(.:format)                   events#destroy
           delete_type GET    /types/:id/delete(.:format)             types#delete
                       DELETE /types/:id/delete(.:format)             types#destroy
                 types GET    /types(.:format)                        types#index
                       POST   /types(.:format)                        types#create
              new_type GET    /types/new(.:format)                    types#new
             edit_type GET    /types/:id/edit(.:format)               types#edit
                  type GET    /types/:id(.:format)                    types#show
                       PATCH  /types/:id(.:format)                    types#update
                       PUT    /types/:id(.:format)                    types#update
                       DELETE /types/:id(.:format)                    types#destroy
delete_event_specialty GET    /event_specialties/:id/delete(.:format) event_specialties#delete
                       DELETE /event_specialties/:id/delete(.:format) event_specialties#destroy
     event_specialties GET    /event_specialties(.:format)            event_specialties#index
                       POST   /event_specialties(.:format)            event_specialties#create
   new_event_specialty GET    /event_specialties/new(.:format)        event_specialties#new
  edit_event_specialty GET    /event_specialties/:id/edit(.:format)   event_specialties#edit
       event_specialty GET    /event_specialties/:id(.:format)        event_specialties#show
                       PATCH  /event_specialties/:id(.:format)        event_specialties#update
                       PUT    /event_specialties/:id(.:format)        event_specialties#update
                       DELETE /event_specialties/:id(.:format)        event_specialties#destroy

规格/特征/ equipment_spec.rb

require 'rails_helper'

RSpec.describe "Equipment", type: :request do
  describe "GET /equipment" do
    it "Adds a new equipment" do
        @types = FactoryGirl.create_list(:multitype, 25)
        @specialties = FactoryGirl.create_list(:multispecialty, 25)
        visit equipment_index_url
        expect{
            click_link 'New Equipment'
            fill_in "Equipment No", with: "2"
            fill_in "Serial", with: "123456"
            fill_in "Description", with: "Test Equipment"
            select "radio10", from: "equipment_type_id"
            select "tech13", from: "equipment_specialty_id"
            click_button "Create Equipment"
        }.to change(Equipment, :count).by(1)
        expect(page).to have_content("Equipment was successfully created")
    end

    it "deletes without javascript" do
        equipment = FactoryGirl.create(:equipment)
        visit equipment_url
        expect {
            click_link "Destroy"
        }.to change(Equipment, :count).by(-1)
        expect(page).to have_content("Equipment was successfully destroyed")
    end

    it "edits a equipment" do
        equipment = FactoryGirl.create(:equipment)
        visit equipment_url
        click_link 'Edit'
        fill_in "Serial", with: "456789"
        click_button "Update Equipment"
        expect(page).to have_content("Equipment was successfully updated")
        expect(page).to have_content("Serial: 456789")
    end

    it "shows a equipment" do
        equipment = FactoryGirl.create(:equipment)
        visit equipment_url
        click_link 'Show'
        expect(page).to have_content("Serial: rad123")
        expect(page).to have_link("Edit")
        expect(page).to have_link("Back", href: equipment_path)
    end
  end
end

错误

Equipment
  GET /equipment
    Adds a new equipment (FAILED - 1)
    deletes without javascript (FAILED - 2)
    edits a equipment (FAILED - 3)
    shows a equipment (FAILED - 4)

故障:

1)设备GET /设备添加新设备      失败/错误:访问equipment_index_url

 NoMethodError:
   undefined method `visit' for #<RSpec::ExampleGroups::Equipment::GETEquipment:0x000000065c32e8>
 # ./spec/features/equipment_spec.rb:8:in `block (3 levels) in <top (required)>'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec/retry.rb:112:in `block in run'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec/retry.rb:101:in `loop'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec/retry.rb:101:in `run'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec_ext/rspec_ext.rb:12:in `run_with_retry'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec/retry.rb:30:in `block (2 levels) in setup'

2)设备GET /设备删除没有javascript      失败/错误:访问equipment_url

 ActionController::UrlGenerationError:
   No route matches {:action=>"show", :controller=>"equipment"} missing required keys: [:id]
 # ./spec/features/equipment_spec.rb:23:in `block (3 levels) in <top (required)>'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec/retry.rb:112:in `block in run'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec/retry.rb:101:in `loop'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec/retry.rb:101:in `run'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec_ext/rspec_ext.rb:12:in `run_with_retry'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec/retry.rb:30:in `block (2 levels) in setup'

3)设备GET /设备编辑设备      失败/错误:访问equipment_url

 ActionController::UrlGenerationError:
   No route matches {:action=>"show", :controller=>"equipment"} missing required keys: [:id]
 # ./spec/features/equipment_spec.rb:32:in `block (3 levels) in <top (required)>'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec/retry.rb:112:in `block in run'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec/retry.rb:101:in `loop'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec/retry.rb:101:in `run'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec_ext/rspec_ext.rb:12:in `run_with_retry'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec/retry.rb:30:in `block (2 levels) in setup'

4)设备GET /设备显示设备      失败/错误:访问equipment_url

 ActionController::UrlGenerationError:
   No route matches {:action=>"show", :controller=>"equipment"} missing required keys: [:id]
 # ./spec/features/equipment_spec.rb:42:in `block (3 levels) in <top (required)>'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec/retry.rb:112:in `block in run'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec/retry.rb:101:in `loop'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec/retry.rb:101:in `run'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec_ext/rspec_ext.rb:12:in `run_with_retry'
 # /home/tom/.rvm/gems/ruby-2.3.1@rails5.0/gems/rspec-retry-0.5.2/lib/rspec/retry.rb:30:in `block (2 levels) in setup'

Finished in 0.28107 seconds (files took 2.61 seconds to load)
4 examples, 4 failures

失败的例子:

rspec ./spec/features/equipment_spec.rb:5 # Equipment GET /equipment Adds a new equipment
rspec ./spec/features/equipment_spec.rb:21 # Equipment GET /equipment deletes without javascript
rspec ./spec/features/equipment_spec.rb:30 # Equipment GET /equipment edits a equipment
rspec ./spec/features/equipment_spec.rb:40 # Equipment GET /equipment shows a equipment

非常感谢任何有关如何使其发挥作用的想法

1 个答案:

答案 0 :(得分:0)

我相信&#34; RSpec.describe&#34;设备&#34;,输入:: request&#34;必须是#34;类型&#34;使水豚测试正常工作。

这应该可以解决显示错误,因为我认为它没有ID,因为它没有正确访问equipment_url。如果这不是问题,那么设备#show没有收到实例变量。