Created
          June 2, 2009 16:23 
        
      - 
      
 - 
        
Save semanticart/122338 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # I'm interested in improvements or variations on doing this in rspec | |
| # assuming we have an instance of ActiveSupport::Cache::MemCacheStore | |
| # as the constant Mem and we want to check to see if a method changes | |
| # the value for a particular key | |
| # this comes into play in the first spec below... | |
| class MemcachedValueFor | |
| def self.method_missing name, *args | |
| Mem.fetch(name) | |
| end | |
| end | |
| describe 'checking that a memcached value is changed' do | |
| before(:each) do | |
| # set our base value | |
| Mem.write('person_name', 'edna') | |
| end | |
| # this is the method we would be testing to ensure that it changes the | |
| # memcached key. | |
| def mystery_method | |
| Mem.write('person_name', 'ralph') | |
| end | |
| # I like this syntax, but using a class w/ method_missing is pretty hacky | |
| it "should be testable via lambda / should change" do | |
| lambda{ | |
| mystery_method | |
| }.should change(MemcachedValueFor, :person_name).to('ralph') | |
| end | |
| # now, this doesn't verify the final result, but do we really want to be | |
| # testing that MemCacheStore works here or just that it gets the message? | |
| # The spec will fail if #write is called more than once, so it should | |
| # cover the bases. | |
| it "should be testable with stubbing" do | |
| Mem.should_receive(:write).with("person_name", "ralph") | |
| mystery_method | |
| end | |
| end | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment