ArgumentError:使用继承初始化时参数的数量错误(0表示为2)

时间:2015-08-10 07:05:02

标签: ruby-on-rails ruby inheritance rspec

好,

这让我疯了,因为我不明白这种情况下的错误。 我定义了以下类:

   private void loadvisitor()
    {

        con1 = new SqlConnection(constring1);
        string seelctquery = "SELECT  M_Status,d.MD_ID , d.MD_Visitor_Name,d.MD_Visitor_Cell, d.MD_Visitor_Company,s.M_StatusName,  p.purpose_name ,  m.M_DateTime FROM  Vms_Meeting M left outer join  Vms_MeetingD  d  on m.M_ID= d.M_ID  left outer join  Vms_Purpose p  on  m.Purpose_ID= p.purpose_id inner join vms_MeetingStatus as s on d.M_Status = s.M_Status WHERE m.M_ID=2 order by m.M_ID desc;";
        da = new SqlDataAdapter(seelctquery, con1);
        SqlCommand cmd = new SqlCommand(seelctquery, con1);

        if (con1.State == ConnectionState.Closed)
        {

            con1.Open();
        }


        dr = cmd.ExecuteReader();
        ds = new DataSet();
        da.Fill(ds);
        cmd.ExecuteNonQuery();
        GridView1.DataSource = ds;
        GridView1.DataBind();


        if (dr.HasRows)
        {
            while (dr.Read())
            {


                string stid = (string)dr["M_Status"];
                if (stid == "1")
                {


                }


            }
        }


        con1.Close();
    }

继承自以下module Admins class BasePresenter < ::BasePresenter def render_customer(id:) return I18n.t('admin.admin') if id.nil? ::Customer.where(id: id).first.try(:name) || I18n.t('admin.deleted') end def percent_of(count, total) ((count.to_f / total.to_f) * 100.0).to_i end end end

BasePresenter

但是当我尝试运行我的规范时,我从RSpec收到以下错误:

  

ArgumentError:参数数量错误(0表示2)   ./app/presenters/base_presenter.rb:3:in class BasePresenter def initialize(object, template) @object = object @template = template end def self.presents(name) define_method(name) do @object end end def underscored_class @object.class.name.underscore end protected def h @template end def handle_none(value, html = true) if value.present? if block_given? yield else value end else return h.content_tag(:span, '-', class: 'none') if html '-' end end def current_customer @current_customer ||= h.current_customer end def current_user @current_user ||= h.current_user end end 块(3   在'

该类与其他礼物没有什么不同,其中继承以完全相同的方式工作,并且这些测试正在通过。 只有在测试方法initialize' ./spec/presenters/admins/base_presenter_spec.rb:24:in时才会对此类的测试失败,并且只有在测试方法percent_of时才会失败。

我没看到什么?

修改

这是我的RSpec测试:

require 'spec_helper'

describe ::Admins::BasePresenter do
  describe '#render_customer' do

    let(:customer) { Customer.first }
    subject { ::Admins::BasePresenter.new(Object.new, ApplicationController.new.view_context) }

    it 'returns the I18n translations for (admin) when no customer is set.' do
      expect(subject.render_customer(id: nil)).to eql(I18n.t('admin.admin'))
    end

    it 'returns the proper name when a valid ID is given' do
      expect(subject.render_customer(id: customer.id)).to eql(customer.name)
    end

    it 'returns the I18n translations for (deleted) when an invalid ID is given' do
      expect(subject.render_customer(id: -1)).to eql(I18n.t('admin.deleted'))
    end
  end

  describe '#percent_of' do
    it 'calculates the percentage correctly' do
      expect(subject.percent_of(0, 1)).to eql(0)
      expect(subject.percent_of(1, 1)).to eql(100)
      expect(subject.percent_of(1, 2)).to eql(50)
      expect(subject.percent_of(1, 3)).to eql(33)
    end
  end
end

1 个答案:

答案 0 :(得分:1)

哎,

我是个白痴.... 问题是我的subject是在Describe块中为特定测试定义的,而第二个没有。 这意味着我们的钩子尝试在外部描述块中创建类的实例...

这是修复:

require 'spec_helper'

describe ::Admins::BasePresenter do
  let(:customer) { Customer.first }
  subject { ::Admins::BasePresenter.new(Object.new, ApplicationController.new.view_context) }

  describe '#render_customer' do
    it 'returns the I18n translations for (admin) when no customer is set.' do
      expect(subject.render_customer(id: nil)).to eql(I18n.t('admin.admin'))
    end

    it 'returns the proper name when a valid ID is given' do
      expect(subject.render_customer(id: customer.id)).to eql(customer.name)
    end

    it 'returns the I18n translations for (deleted) when an invalid ID is given' do
      expect(subject.render_customer(id: -1)).to eql(I18n.t('admin.deleted'))
    end
  end

  describe '#percent_of' do
    it 'calculates the percentage correctly' do
      expect(subject.percent_of(0, 1)).to eql(0)
      expect(subject.percent_of(1, 1)).to eql(100)
      expect(subject.percent_of(1, 2)).to eql(50)
      expect(subject.percent_of(1, 3)).to eql(33)
    end
  end
end