2015-09-02 77 views
3

我想用rspec測試我的註冊視圖。如何用rspec測試設計視圖

我已經嘗試了很多事情喜歡這裏建議Testing devise views with rspecTesting Views that use Devise with RSpec

但似乎沒有什麼是確定的,我總是得到這樣的錯誤:

undefined local variable or method `resource' 

does not implement: resource 

我當前規格(spec/views/devise/registrations/new.slim_spec.rb):

require 'rails_helper' 

RSpec.describe 'devise/registrations/new.slim', type: :view do 

    describe 'sign up form' do 

    before do 
     allow(view).to receive(:resource).and_return(User.new) 
     allow(view).to receive(:resource_name).and_return(:user) 
    end 

    it 'does not contains an area for user avatar' do 
     render 
     expect(rendered).to_not include('Profile Pic') 
    end 
    end 
end 

我的環境:

  • 軌4.2.3
  • 色器件3.5.1
  • rspec的3.3.0

我在做什麼錯?謝謝你的幫助。

編輯:

我將我的代碼到控制器的規格和它的作品,並不完美,但......

RSpec.describe Users::RegistrationsController, type: :controller do 
    before(:each) do 
    request.env['devise.mapping'] = Devise.mappings[:user] 
    end 

    describe '#new' do 
    render_views 

    it 'does not contains an area for user avatar' do 
     get :new 
     expect(response.body).to_not include('Profile Pic') 
    end 
    end 
end 
+1

您是否嘗試過維基頁面? https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara –

+1

你應該發佈一些代碼來展示你的嘗試。 –

+0

@ruby_newbie我剛剛添加了我的規範。 – user3671545

回答