开发者

Why am I getting the error "undefined local variable or method `assigns'"?

I might be missing something basic here, but I'm stumped on this error:

model code:

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

spec code:

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

The error is:

undefined local variable or method `assigns' for #<Spec::Rails::Example::ModelExampleGroup::Subclass_1:0x2f开发者_StackOverflow中文版bac28>

I feel like I'm overlooking something obvious but it's invisible to me. Any suggestions?

Thanks very much! -Jason


as neutrino said, assigns are only available in controllers/views specs, they're meaningless in a Model specs.

in your case example can look like

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


I could be wrong here, but assigns might be available only in controller specs.

Also, check your rspec version.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜