Wzorzec Obserwator - otrzymywanie subskrybowanych informacji.

Po tym jak mogę dodawać obiekty do listy subskrybentów zabieram się za otrzymywanie wiadomości.

Poprzedni post tu.

Napisałem sobie krótki test w Ruby:
def test_receiving_information                                                                                                                                                                                                             
                                                                                                                                                                                                                                             
    @client = Client.new                                                                                                                                                                                                                     
    @media.add_advertising("5 55 555")                                                                                                                                                                                                       
    @media.add_to_subscribers @client                                                                                                                                                                                                        
    #@media.broadcast                                                                                                                                                                                                                        
    #assert_not_empty @client.ads                                                                                                                                                                                                            
                                                                                                                                                                                                                                             
end




Na razie wykomentowałem dalsze akcji i puszczam rake:

coola@sv26 [/home/coola/dsp2017/xp-simulator]# rake                                                                                                                                                                                          
/usr/share/ruby-2.3.1/bin/ruby ./test/all_tests.rb                                                                                                                                                                                           
Loaded suite ./test/all_tests                                                                                                                                                                                                                
Started                                                                                                                                                                                                                                      
......E                                                                                                                                                                                                                                      
=============================================================================================================================================================================================================================================
Error: test_receiving_information(SubscriptionTests): RuntimeError: Argument should have publish method                                                                                                                                      
/home/coola/dsp2017/xp-simulator/src/Media.rb:17:in `add_to_subscribers'                                                                                                                                                                     
/home/coola/dsp2017/xp-simulator/test/subscription_tests.rb:47:in `test_receiving_information'                                                                                                                                               
     44:                                                                                                                                                                                                                                     
     45:     @client = Client.new                                                                                                                                                                                                            
     46:     @media.add_advertising("5 55 555")                                                                                                                                                                                              
  => 47:     @media.add_to_subscribers @client                                                                                                                                                                                               
     48:     #@media.broadcast                                                                                                                                                                                                               
     49:     #assert_not_empty @client.ads                                                                                                                                                                                                   
     50:                                                                                                                                                                                                                                     
=============================================================================================================================================================================================================================================
                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                             
Finished in 0.002118131 seconds.                                                                                                                                                                                                             
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
7 tests, 9 assertions, 0 failures, 1 errors, 0 pendings, 0 omissions, 0 notifications                                                                                                                                                        
85.7143% passed                                                                                                                                                                                                                              
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3304.80 tests/s, 4249.03 assertions/s                                                                                                                                                                                                        
rake aborted!                                                                                                                                                                                                                                
Command failed with status (1): [/usr/share/ruby-2.3.1/bin/ruby ./test/all_...]                                                                                                                                                              
/home/coola/dsp2017/xp-simulator/Rakefile:4:in `block in <top (required)>'                                                                                                                                                                   
/home/coola/.gems/2.3/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'                                                                                                                                                                     
Tasks: TOP => default => test                                                                                                                                                                                                                
(See full trace by running task with --trace)                                   


Okazuje się, że mój klient nie "implementuje" metody publish, potrzebnej dla obiektu Media do publikacji.

Dopisuję dwie linijki:

class Client                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                             
  def publish                                                                                                                                                                                                                                
  end  
                                                                                                                                                                                                                                      
end 

Dla rake to wystarczy:

coola@sv26 [/home/coola/dsp2017/xp-simulator]# rake                                                                                                                                                                                          
/usr/share/ruby-2.3.1/bin/ruby ./test/all_tests.rb                                                                                                                                                                                           
Loaded suite ./test/all_tests                                                                                                                                                                                                                
Started                                                                                                                                                                                                                                      
.......                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                             
Finished in 0.00171854 seconds.                                                                                                                                                                                                              
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
7 tests, 9 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications                                                                                                                                                        
100% passed                                                                                                                                                                                                                                  
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4073.22 tests/s, 5237.00 assertions/s               


Odkomentowuję następną linijkę:

def test_receiving_information                                                                                                                                                                                                             
                                                                                                                                                                                                                                             
    @client = Client.new                                                                                                                                                                                                                     
    @media.add_advertising("5 55 555")                                                                                                                                                                                                       
    @media.add_to_subscribers @client                                                                                                                                                                                                        
    @media.broadcast                                                                                                                                                                                                                         
    #assert_not_empty @client.ads                                                                                                                                                                                                            
                                                                                                                                                                                                                                             
end 


Tutaj przede wszystkim nie ma metody broadcast i jej implementacji.

coola@sv26 [/home/coola/dsp2017/xp-simulator]# rake                                                                                                                                                                                          
/usr/share/ruby-2.3.1/bin/ruby ./test/all_tests.rb                                                                                                                                                                                           
Loaded suite ./test/all_tests                                                                                                                                                                                                                
Started                                                                                                                                                                                                                                      
......E                                                                                                                                                                                                                                      
=============================================================================================================================================================================================================================================
Error: test_receiving_information(SubscriptionTests): NoMethodError: undefined method `broadcast' for #<Media:0x007f3f412768c0>                                                                                                              
/home/coola/dsp2017/xp-simulator/test/subscription_tests.rb:48:in `test_receiving_information'                                                                                                                                               
     45:     @client = Client.new                                                                                                                                                                                                            
     46:     @media.add_advertising("5 55 555")                                                                                                                                                                                              
     47:     @media.add_to_subscribers @client                                                                                                                                                                                               
  => 48:     @media.broadcast                                                                                                                                                                                                                
     49:     #assert_not_empty @client.ads                                                                                                                                                                                                   
     50:                                                                                                                                                                                                                                     
     51:   end                                                                                                                                                                                                                               
=============================================================================================================================================================================================================================================
                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                             
Finished in 0.002872458 seconds.                                                                                                                                                                                                             
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
7 tests, 9 assertions, 0 failures, 1 errors, 0 pendings, 0 omissions, 0 notifications                                                                                                                                                        
85.7143% passed                                                                                                                                                                                                                              
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2436.94 tests/s, 3133.21 assertions/s                                                                                                                                                                                                        
rake aborted!                                                                                                                                                                                                                                
Command failed with status (1): [/usr/share/ruby-2.3.1/bin/ruby ./test/all_...]                                                                                                                                                              
/home/coola/dsp2017/xp-simulator/Rakefile:4:in `block in <top (required)>'                                                                                                                                                                   
/home/coola/.gems/2.3/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'                                                                                                                                                                     
Tasks: TOP => default => test                                                                                                                                                                                                                
(See full trace by running task with --trace)          


Dodaję zmiany w klasie Media:

class Media                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                             
  attr_accessor :ads, :subscribers                                                                                                                                                                                                           
                                                                                                                                                                                                                                             
  def initialize                                                                                                                                                                                                                             
    @ads = Array.new                                                                                                                                                                                                                         
    @subscribers = Array.new                                                                                                                                                                                                                 
  end                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                             
  def add_advertising telephone_number                                                                                                                                                                                                       
    @ads.push telephone_number                                                                                                                                                                                                               
  end                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                             
  def add_to_subscribers client                                                                                                                                                                                                              
    if !client.respond_to? :publish                                                                                                                                                                                                          
      raise 'Argument should have publish method'                                                                                                                                                                                            
    end                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                             
    @subscribers.push client                                                                                                                                                                                                                 
                                                                                                                                                                                                                                             
  end                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                             
  def broadcast                                                                                                                                                                                                                              
    @subscribers.each { |subscriber| subscriber.publish @ads }                                                                                                                                                                               
  end                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                             
end 


Używam metody publish z atrybutem i rzeczywiście rake mówi, że w klasie Client nie ma atrybutu metody publish:

                                                                                                                                                                                         
Loaded suite ./test/all_tests                                                                                                                                                                                                                
Started                                                                                                                                                                                                                                      
......E                                                                                                                                                                                                                                      
=============================================================================================================================================================================================================================================
Error: test_receiving_information(SubscriptionTests): ArgumentError: wrong number of arguments (given 1, expected 0)                                                                                                                         
/home/coola/dsp2017/xp-simulator/src/Client.rb:3:in `publish'                                                                                                                                                                                
/home/coola/dsp2017/xp-simulator/src/Media.rb:25:in `block in broadcast'                                                                                                                                                                     
/home/coola/dsp2017/xp-simulator/src/Media.rb:25:in `each'                                                                                                                                                                                   
/home/coola/dsp2017/xp-simulator/src/Media.rb:25:in `broadcast'                                                                                                                                                                              
/home/coola/dsp2017/xp-simulator/test/subscription_tests.rb:48:in `test_receiving_information'                                                                                                                                               
     45:     @client = Client.new                                                                                                                                                                                                            
     46:     @media.add_advertising("5 55 555")                                                                                                                                                                                              
     47:     @media.add_to_subscribers @client                                                                                                                                                                                               
  => 48:     @media.broadcast                                                                                                                                                                                                                
     49:     #assert_not_empty @client.ads                                                                                                                                                                                                   
     50:                                                                                                                                                                                                                                     
     51:   end                                                                                                                                                                                                                               
=============================================================================================================================================================================================================================================
                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                             
Finished in 0.002341671 seconds.                                                                                                                                                                                                             
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
7 tests, 9 assertions, 0 failures, 1 errors, 0 pendings, 0 omissions, 0 notifications                                                                                                                                                        
85.7143% passed                                                                                                                                                                                                                              
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2989.32 tests/s, 3843.41 assertions/s                                                                                                                                                                                                        
rake aborted!                                                                                                                                                                                                                                
Command failed with status (1): [/usr/share/ruby-2.3.1/bin/ruby ./test/all_...]                                                                                                                                                              
/home/coola/dsp2017/xp-simulator/Rakefile:4:in `block in <top (required)>'                                                                                                                                                                   
/home/coola/.gems/2.3/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'                                                                                                                                                                     
Tasks: TOP => default => test                                                                                                                                                                                                                
(See full trace by running task with --trace)           

Dopisuję argument ads do klasy Client i jest OK.

Teraz odkomentowuję ostatnią linijkę testu:

def test_receiving_information                                                                                                                                                                                                             
                                                                                                                                                                                                                                             
    @client = Client.new                                                                                                                                                                                                                     
    @media.add_advertising("5 55 555")                                                                                                                                                                                                       
    @media.add_to_subscribers @client                                                                                                                                                                                                        
    @media.broadcast                                                                                                                                                                                                                         
    assert_not_empty @client.ads                                                                                                                                                                                                             
                                                                                                                                                                                                                                             
  end  

Rake krzyczy, że nie ma zmiennej klasowej ads i jest ona pusta:

Started                                                                                                                                                                                                                                      
......E                                                                                                                                                                                                                                      
=============================================================================================================================================================================================================================================
Error: test_receiving_information(SubscriptionTests): NoMethodError: undefined method `ads' for #<Client:0x007f1a96ef2420>                                                                                                                   
/home/coola/dsp2017/xp-simulator/test/subscription_tests.rb:49:in `test_receiving_information'                                                                                                                                               
     46:     @media.add_advertising("5 55 555")                                                                                                                                                                                              
     47:     @media.add_to_subscribers @client                                                                                                                                                                                               
     48:     @media.broadcast                                                                                                                                                                                                                
  => 49:     assert_not_empty @client.ads                                                                                                                                                                                                    
     50:                                                                                                                                                                                                                                     
     51:   end                                                                                                                                                                                                                               
     52:                                                                                                                                                                                                                                     
=============================================================================================================================================================================================================================================
                         

Dodaję zmiany:

class Client                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                             
  def publish ads                                                                                                                                                                                                                            
                                                                                                                                                                                                                                             
    @ads = ads                                                                                                                                                                                                                               
                                                                                                                                                                                                                                             
  end                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                             
  def ads                                                                                                                                                                                                                                    
    @ads                                                                                                                                                                                                                                     
  end                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                             
end

I teraz projekt przechodzi wszystkie testy :)


coola@sv26 [/home/coola/dsp2017/xp-simulator]# rake                                                                                                                                                                                          
/usr/share/ruby-2.3.1/bin/ruby ./test/all_tests.rb                                                                                                                                                                                           
Loaded suite ./test/all_tests                                                                                                                                                                                                                
Started                                                                                                                                                                                                                                      
.......                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                             
Finished in 0.001713314 seconds.                                                                                                                                                                                                             
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
7 tests, 10 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications                                                                                                                                                       
100% passed                                                                                                                                                                                                                                  
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4085.65 tests/s, 5836.64 assertions/s 

Commit do tego tu.

To jest ostatni projektowy wpis w konkursie Daj się Poznać 2017. Dzięki Maćku i Społeczności :)

Brak komentarzy:

Prześlij komentarz