【问题标题】:Why am I getting the error "undefined local variable or method `assigns'"?为什么我收到错误“未定义的局部变量或方法‘assigns’”?
【发布时间】:2010-06-10 23:10:13
【问题描述】:

我可能在这里遗漏了一些基本的东西,但我被这个错误难住了:

型号代码:

class CachedStat < ActiveRecord::Base
    def self.create_stats_days_ago(days_ago, human_id)
    d = Date.today - days_ago.day
    @prs = PageRequest.find(:all, :conditions => [ "owner_type = 'Human' and owner_id = ? and created_at = ?", human_id, d] )
  end
end

规格代码:

it "should create stats for the specified number of days in the past" do
  CachedStat.create_stats_days_ago(1, Human.first.id)
  assigns[:prs].should eql("foo")
end

错误是:

undefined local variable or method `assigns' for #<Spec::Rails::Example::ModelExampleGroup::Subclass_1:0x2fbac28>

我觉得我忽略了一些明显的东西,但它对我来说是不可见的。有什么建议吗?

非常感谢! -杰森

【问题讨论】:

    标签: ruby-on-rails rspec


    【解决方案1】:

    正如 neutrino 所说,assigns 仅在控制器/视图规范中可用,它们在模型规范中毫无意义。

    你的例子看起来像

    it "should create stats for the specified number of days in the past" do
      CachedStat.create_stats_days_ago(1, Human.first.id).should eql("foo")
    end
    

    【讨论】:

      【解决方案2】:

      我在这里可能是错的,但assigns 可能仅在控制器规格中可用。

      另外,请检查您的 rspec 版本。

      【讨论】:

        猜你喜欢
        • 2015-05-16
        • 2012-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-06
        • 1970-01-01
        • 1970-01-01
        • 2017-01-17
        相关资源
        最近更新 更多