require 'test_helper' class StreamitTest < ActiveSupport::TestCase def stream_store Streamit.store end def wait_and_finish Streamit.finish! end context "when user watches an item" do setup do TestWatching.class_eval do stream :create, :actor => :watcher, :subject => :watched_item end @watch = TestWatching.new(:watcher => TestUser.create!, :watched_item => TestItem.create!) end should "create a stream if watching is created successfully" do @watch.save wait_and_finish stream = stream_store.last assert_not_nil stream assert_equal "streamit.testwatchings.create", stream.stream_type assert_equal @watch.watcher, stream.actor assert_equal @watch.watched_item, stream.subject assert_nil stream.receiver end should "not create a stream if failed to create a watching" do TestWatching.class_eval do before_create { return false } end assert_no_difference("stream_store.count") { @watch.save; wait_and_finish } end end context "when create or update a user" do setup do @user = TestUser.new @user.singleton_class.class_eval do def self.name; "TestUser"; end end end should "create a stream" do @user.singleton_class.class_eval do stream :create end @user.save wait_and_finish stream = stream_store.last assert_not_nil stream assert_equal "streamit.testusers.create", stream.stream_type assert_equal @user, stream.actor assert_nil stream.subject assert_nil stream.receiver end context "when user updates her attributes" do setup do @user.save wait_and_finish end should "create a stream the attribute specified has changed" do @user.singleton_class.class_eval do stream :update, :attributes => :image_url end @user.image_url = "image" @user.save wait_and_finish stream = stream_store.last assert_not_nil stream assert_equal "streamit.testusers.update.image_url", stream.stream_type assert_equal @user, stream.actor assert_nil stream.subject assert_nil stream.receiver end should "create a stream if any attributes have changed" do @user.singleton_class.class_eval do stream :update end @user.name = "davis" @user.save wait_and_finish stream = stream_store.last assert_not_nil stream assert_equal "streamit.testusers.update.default", stream.stream_type assert_equal @user, stream.actor assert_nil stream.subject assert_nil stream.receiver end should "create a stream if any attributes specified have changed" do @user.singleton_class.class_eval do stream :update, :attributes => [:name, :image_url] end @user.name = "davis" @user.save wait_and_finish stream = stream_store.last assert_not_nil stream assert_equal "streamit.testusers.update.default", stream.stream_type assert_equal @user, stream.actor assert_nil stream.subject assert_nil stream.receiver end should "not create a stream if no attributes has changed" do assert_no_difference("stream_store.count") { @user.save } end should "not create a stream if no attributes specified has changed" do @user.singleton_class.class_eval do stream :update, :attributes => :name end @user.image_url = "image" @user.save wait_and_finish assert_equal 0, stream_store.count end end end end