require 'test_helper' if Cell.rails_version >= 3.1 class Song < OpenStruct extend ActiveModel::Naming def persisted? true end def to_param id end end class SongCell < Cell::ViewModel def show render end def title song.title.upcase end def self_url url_for(song) end def details render end def stats render :details end def info render :info end def dashboard render :dashboard end def scale render :layout => 'b' end class Lyrics < self def show render :lyrics end end class PlaysCell < self def show render :plays end end end class ViewModelTest < MiniTest::Spec # views :show, :create #=> wrap in render_state(:show, *) let (:cell) { SongCell.new(nil, :title => "Shades Of Truth") } it { cell.title.must_equal "Shades Of Truth" } class HitCell < Cell::ViewModel property :title, :artist def show "Great!" end def rate "Fantastic!" end attr_accessor :count cache :count def title super.upcase end end let (:song) { Song.new(:title => "Sixtyfive", artist: "Boss") } # ::property creates accessor. it { HitCell.new(nil, song).artist.must_equal "Boss" } # ::property accessor can be overridden and call super. it { HitCell.new(nil, song).title.must_equal "SIXTYFIVE" } describe "#call" do let (:cell) { HitCell.new(nil, song) } it { cell.call.must_equal "Great!" } it { cell.call(:rate).must_equal "Fantastic!" } it "no caching" do cell.instance_eval do def cache_configured? false end end cell.count = 1 cell.call(:count).must_equal "1" cell.count = 2 cell.call(:count).must_equal "2" end it "with caching" do cell.instance_eval do self.cache_store = ActiveSupport::Cache::MemoryStore.new self.cache_configured = true end cell.count = 1 cell.call(:count).must_equal "1" cell.count = 2 cell.call(:count).must_equal "1" end # call(:show) do .. end it do cell.call(:show) do |c| c.instance_variable_set(:@volume, 9) end.must_equal "Great!" cell.instance_variable_get(:@volume).must_equal 9 end end # describe "::helper" do # it { assert_raises { HitCell.helper Module.new } } # end end if Cell.rails_version >= "3.2" class ViewModelIntegrationTest < ActionController::TestCase tests MusicianController #let (:song) { Song.new(:title => "Blindfold", :id => 1) } #let (:html) { %{