Assert that the method is not called on the layout

I am writing tests with Rspec2 using a flash-based flash-based framework. I expect one of my methods to cache the results and want to test this using my layout.

describe SomeClass do
  before do
    @mock = flexmock()
  end

  after do
    @mock.flexmock_verify()
  end

  it "method caches results"
    c = SomeClass.new(@mock)
    c.method
    @mock.should_receive(:expensive_method).never
    c.method.should == ['A']
  end
end

This works well if I want to make sure it is :expensive_methodnever called. However, I expect my class to be able to do :methodwithout invoking anything from the class (mock). Is there any way to express this?

Background: . In my case, the class being introduced performs expensive operations, so the results should be cached for equal queries.


, , , , , , . , , - SomeClass :method:

def SomeClass
  @@cache_map = {}

  def method
    # extract key
    return @@cache_map[key] if @@cache_map.has_key?(key)
    # call :expensive_method to get result
    @@cache_map[key] = result
    return result
  end
end

, , :extensive_method . , , : , SomeClass.method ( :expensive_method, - ).

+3
2

, @mock.should_receive(:expensive_method).once method , , .

: , , , :

describe Client do
  let(:service) { flexmock() }
  let(:client)  { Client.new(service) }

  it "uses data retrieved from service" do
    service.stub(:expensive_method).and_return('some data')
    client.method.should eq('some data')
  end

  it "only retrieves the data once" do
    service.should_receive(:expensive_method).once
    client.method
    client.method
  end
end

, flexmock_verify, .

+9

, . , mock, ( , Flexmock, , , ).

, . , , , , - " ".

+1

Source: https://habr.com/ru/post/1778330/


All Articles