相关疑难解决方法(0)

Ruby on Rails,包括带参数的模块

在包含ruby模块时有没有办法使用参数?我有一个模块Assetable,它包含在许多类中.我希望能够动态生成attr_accessor.

module Assetable
  extend ActiveSupport::Concern

  included do 
    (argument).times do |i| 
      attr_accessor "asset_#{i}".to_sym
      attr_accessible "asset_#{i}".to_sym
    end
  end
end 
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails

11
推荐指数
4
解决办法
8036
查看次数

如何使用 rspec 测试设计视图

我想用 rspec 测试我的注册视图。

我已经尝试了很多事情,比如这里建议的使用rspec 测试设计视图使用 RSpec 的设计测试视图

但似乎没有什么问题,我总是收到这样的错误:

undefined local variable or method `resource'
Run Code Online (Sandbox Code Playgroud)

或者

does not implement: resource
Run Code Online (Sandbox Code Playgroud)

我当前的规格(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 
Run Code Online (Sandbox Code Playgroud)

我的环境:

  • 导轨 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 …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails devise

6
推荐指数
1
解决办法
4624
查看次数

标签 统计

ruby-on-rails ×2

devise ×1

rspec ×1

ruby ×1