gpt4 book ai didi

ruby-on-rails - 如何使用 rspec 测试设计 View

转载 作者:行者123 更新时间:2023-12-04 05:38:32 25 4
gpt4 key购买 nike

我想用 rspec 测试我的注册 View 。

我已经尝试了很多类似这里建议的方法 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
  • 设计 3.5.1
  • rspec 3.3.0

  • 我究竟做错了什么?谢谢你的帮助。

    编辑:

    我将我的代码移动到 Controller 规范并且它可以工作,并不完美但是......
    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

    最佳答案

    关于ruby-on-rails - 如何使用 rspec 测试设计 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32358049/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com