0 Replies - 1237 Views - Last Post: 24 November 2015 - 06:24 AM Rate Topic: -----

#1 Trance123  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 125
  • Joined: 20-September 13

RSpec testing

Posted 24 November 2015 - 06:24 AM

Hi

Im having a hard time getting this RSpec test to pass. Can anyone here spot my problems?

Here is the Method

 def author_search author
        result = nil
        memcached_isbns = @Remote_cache.get "bks_#{author}"
        if memcached_isbns
           isbn_array = memcached_isbns.split(',')
           complex_object_key_parts = isbn_array.map do |isbn|
              build_ISBN_version_string isbn, nil
           end
           key = "#{author}_#{complex_object_key_parts.join('_')} "
           value = @Remote_cache.get key
           if value
              result = JSON.parse value
           else
              books = complex_object_key_parts.map do |element| 
                 Book.from_cache(@Remote_cache.get element)
              end
              result = compute_author_report books
              @Remote_cache.set key,result.to_json
           end
        else
          books = @database.authorSearch author
          @Remote_cache.set "bks_#{author}", 
                         (books.map{|book| book.isbn }).join(',')
          complex_object_key_parts = books.map do |book|
               build_ISBN_version_string book.isbn, book
          end
          key = "#{author}_#{complex_object_key_parts.join('_')} "
          result = compute_author_report books
          @Remote_cache.set key,result.to_json
        end
        result
    end


Here is the test

 describe '#author_search' do
              context 'when books author is present in the remote cache' do
                it 'it should call JSON parse' do
                  database_with_cache = DatabaseWithCache.new(database, remote_cache)

                  allow(remote_cache).to receive(:get).with("bks_#{book.author}").and_return(memcache_copy)
                  allow(remote_cache).to receive(:get).and_return(memcache_version)
                  allow(remote_cache).to receive(:get).with(book.author + '_' + memcache_copy).and_return(memcache_version)
                  expect(JSON).to receive(:parse).with(memcache_version).and_return(memcache_version)

                  result = database_with_cache.author_search(book.author)
                  expect(result).to eq(memcache_version)
                end

                it 'should set data in the remote cache' do
                  database_with_cache = DatabaseWithCache.new(database, remote_cache)

                  allow(remote_cache).to receive(:get).with("bks_#{book.author}").and_return(memcache_copy)
                  expect_any_instance_of(DatabaseWithCache).to receive(:build_sbn_version_string).exactly(6).times.and_return(memcache_version)
                  allow(remote_cache).to receive(:get).with("#{book.author}_1_1_1_1_1_1 ").and_return(false)
                  allow(remote_cache).to receive(:get).with(memcache_version).and_return(memcache_copy)
                  expect(remote_cache).to receive(:set).with("#{book.author}_1_1_1_1_1_1 ", { books: [{title: 'title', isbn: 'isbn'}]*6, value: book.quantity*book.price*6 }.to_json)

                  result = database_with_cache.author_search(book.author)
                  expect(result['books'].count).to eq(6)
                  expect(result).to eq({ "books" => [{'title' => 'title', 'isbn' => 'isbn'}]*6, 'value' => book.quantity*book.price*6 })
                end
              end

              context 'when bks author is missing in the remote cache' do
                it 'should set data in the remote cache' do

                  database_with_cache = DatabaseWithCache.new(database, remote_cache)

                  allow(remote_cache).to receive(:get).with("bks_#{book.author}").and_return(false)
                  allow(database).to receive(:author_search).with(book.author){ [book]*2 }
                  expect(remote_cache).to receive(:set).with("bks_#{book.author}", ([book.isbn]*2).join(','))
                  expect_any_instance_of(DatabaseWithCache).to receive(:build_sbn_version_string).exactly(2).times.and_return(memcache_version)
                  expect(remote_cache).to receive(:set).with("#{book.author}_1_1 ", { books: [{title: 'title', isbn: 'isbn'}]*2, value: book.quantity*book.price*2 }.to_json)

                  result = database_with_cache.author_search(book.author)
                  expect(result['books'].count).to eq(2)
                  expect(result).to eq({ "books" => [{'title' => 'title', 'isbn' => 'isbn'}]*2, 'value' => book.quantity*book.price*2 })

                  
                end
              end
            end


It is failing on

Failed examples:

rspec ./test_database_with_cache.rb:100 # DatabaseWithCache#isbn_search Given the book ISBN is valid #author_search when books author is present in the remote c
ache should set data in the remote cache rspec ./test_database_with_cache.rb:116 # DatabaseWithCache#isbn_search Given the book ISBN is valid #author_search when bks author is missing in the remote cache should set data in the remote cache



Is This A Good Question/Topic? 0
  • +

Page 1 of 1