我想用rspec测试我的注册视图。
我尝试了很多方法,如这里建议的Testing devise views with rspec或者Testing Views that use Devise with RSpec。
但是似乎都不行,我总是会收到错误提示:
我尝试了很多方法,如这里建议的Testing devise views with rspec或者Testing 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
我的环境:
- rails 4.2.3
- devise 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