2016-07-28 83 views
0

我試圖建立一個簡單的博客使用導軌4和身份驗證我使用設計。Rspec:在設計中測試註冊路由在導軌

博客很簡單,它只有一個註冊用戶,即管理員。所以當數據庫中沒有用戶時,註冊頁面應該可用,否則它將重定向到主頁。 我正在嘗試學習過程中的測試。所以我開始測試RegistrationContrroler然而它似乎沒有通過。

class RegistrationsController < Devise::RegistrationsController  

    before_action :one_user_registered?, only: [:new, :create] 

    protected 

    def one_user_registered? 
    if ((User.count == 1) & (user_signed_in?)) 
     redirect_to root_path 
    elsif User.count == 1 
     redirect_to new_user_session_path 
    end 
    end 

end 

規格:

RSpec.describe RegistrationsController, type: :controller do 

    context "Admin already registred" do 
    it "cannot create new user" do 
     @user_attr = FactoryGirl.attributes_for(:user) 
     # Create Admin 
     User.create!(@user_attr) 
     @request.env["devise.mapping"] = Devise.mappings[:user] 
     post :create, user: @user_attr 
     User.count should eq_to(1) 

    end 
    end 
end 

spec/factories.rb

FactoryGirl.define do 

    factory :user do 
    sequence(:email) { |n| "user#{n}@example.com" } 
    password "secure" 
    end 

    factory :post do 
    sequence(:id){ |n| n } 
    sequence(:title) { |n| "Post title #{n}" } 
    body "Post body" 
    sequence(:slug) { |n| "Post title #{n}" } 
    end 
end 

的routes.rb

Rails.application.routes.draw do 

    devise_for :users, controllers: { registrations: "registrations" } 

    authenticated :user do 
    resources :posts 
    end 

    unauthenticated :user do 
     resources :posts, only: [:show, :index] 
    end 

    root 'posts#index' 
end 

堆棧跟蹤:

F 

Failures: 

    1) RegistrationsController Admin already registred cannot create new user 
    Failure/Error: User.count should eq_to(1) 

     expected: 1 
      got: #<RegistrationsController:0x00000003a413b8 @_action_has_layout=true, @_routes=nil, @_headers={"Conten...enticatable]>, @req touest_format=:html, @marked_for_same_origin_verification=false, @current_user=nil> 

     (compared using ==) 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-support-3.5.0/lib/rspec/support.rb:87:in `block in <module:Support>' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-support-3.5.0/lib/rspec/support.rb:96:in `notify_failure' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/fail_with.rb:27:in `fail_with' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/handler.rb:38:in `handle_failure' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/handler.rb:50:in `block in handle_matcher' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/handler.rb:27:in `with_matcher' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/handler.rb:48:in `handle_matcher' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/memoized_helpers.rb:81:in `should' 
    # ./spec/controllers/registrations_controller_spec.rb:13:in `block (3 levels) in <top (required)>' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:252:in `instance_exec' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:252:in `block in run' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:494:in `block in with_around_and_singleton_context_hooks' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:451:in `block in with_around_example_hooks' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:471:in `block in run' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:611:in `block in run_around_example_hooks_for' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:336:in `call' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-rails-3.5.1/lib/rspec/rails/example/controller_example_group.rb:191:in `block (2 levels) in <module:ControllerExampleGroup>' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:441:in `instance_exec' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:441:in `instance_exec' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:382:in `execute_with' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:613:in `block (2 levels) in run_around_example_hooks_for' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:336:in `call' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-rails-3.5.1/lib/rspec/rails/adapters.rb:127:in `block (2 levels) in <module:MinitestLifecycleAdapter>' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:441:in `instance_exec' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:441:in `instance_exec' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:382:in `execute_with' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:613:in `block (2 levels) in run_around_example_hooks_for' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:336:in `call' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:614:in `run_around_example_hooks_for' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:471:in `run' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:451:in `with_around_example_hooks' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:494:in `with_around_and_singleton_context_hooks' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:249:in `run' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:627:in `block in run_examples' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:623:in `map' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:623:in `run_examples' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:589:in `run' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:590:in `block in run' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:590:in `map' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:590:in `run' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:113:in `block (3 levels) in run_specs' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:113:in `map' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:113:in `block (2 levels) in run_specs' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/configuration.rb:1836:in `with_suite_hooks' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:112:in `block in run_specs' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/reporter.rb:77:in `report' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:111:in `run_specs' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:87:in `run' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:71:in `run' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:45:in `invoke' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/exe/rspec:4:in `<top (required)>' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/bin/rspec:23:in `load' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/bin/rspec:23:in `<main>' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval' 
    # /home/chaker/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>' 

Finished in 0.08188 seconds (files took 1.95 seconds to load) 
1 example, 1 failure 

Failed examples: 

rspec ./spec/controllers/registrations_controller_spec.rb:7 # RegistrationsController Admin already registred cannot create new user 
+0

'((User.count == 1)&(user_signed_in?))'this should be'(User.count == 1 && user_signed_in?)' –

+0

@PardeepDhingra就我所知,這並沒有什麼區別。這不是優先問題。 – Chaker

+0

@PardeepDhingra是對的,雖然......'&'不正確,'&&'是正確的。 – SteveTurczyn

回答

1

你錯過了一個點!

User.count.should eq_to(1) 

添加should方法是這樣的RSpec的補丁的所有對象,支持should比較。

優選的是...

expect(User.count).to eq(1) 

錯誤表明,should本身是由RegistrationsController收到的,而不是由User.count ...當然還有RegistrationsController不等於1

+0

事實上,我想我需要更多關於這個主題的閱讀。謝謝! – Chaker