DISQUS

OLD Bamboo Blog: Defining interfaces through mocking

  • BrianC · 2 years ago
    I appreciate that you started all the way back at the 'gem install rspec'. This was a good intro for me. Thanks for taking the time to write it up. I look forward to pt 2.

    One hiccup I ran into was that you never explicitly point out that we need to execute the migration. Of course, it's obvious now, but when you're focusing on shiny new things, the obvoius can go unnoticed :)

    Anyway, if anyone else gets:

    ActiveRecord::StatementInvalid in 'A general auction should have errors given invalid values'
    ActiveRecord::StatementInvalid
    ./spec/models/auction_spec.rb:31:in `new'
    ./spec/models/auction_spec.rb:31:

    just run 'rake db:migrate' and all becomes well.

    Thanks again.
  • Mark Bennett · 2 years ago
    You created a mock and a stub but you didn't use any of the capabilities of mocking except for creating a stub that you didn't use.
  • Mark Bennett · 2 years ago
    OK, I missed the :save stub and only saw the :new_record one.
  • Eric ALlam · 2 years ago
    Mark, great introduction to Rspec and stubbing/mocking. I have recently fallen for Rspec and stubbing because, as you said, it makes our interfaces better.

    One thing that caught be attention were your expectations. For instance:

    @auction.valid?.should_equal false

    Could be written like this:

    @auction.should_not_be_valid

    Rspec is clever enough (using method_missing) to run the method :valid? on the @auction object and make sure its false (because of the should_not). I find its cleaner that way and it will also take advantage of Rspec's new generated spec names feature.
  • James H. · 2 years ago
    Unless I missed something, in step 9 your last bit of code should be:

    <pre>

    specify "should be able to deactivate auction" do
    @auction.attributes = AuctionSpecHelper.valid_auction_attributes
    @auction.deactivate!
    @auction.activated?.should_equal false
    end

    </pre>
  • jonathan Conway · 2 years ago
    @james/@brian - Ah I knew I forgot something:)

    @eric - Easy tiger, I'll get onto different expectation vocabulary together with more advanced parts of RSpec and use of mocking in the next article.:)
  • Anand Fox · 2 years ago
    Thanks for the great article.

    I had to add the column 'owner' to the auction model to get the last test:
    "context "An auction with an owner" do"
    to pass. Otherwise it was failing with no such method owner= for the auction model class.

    Is this necessary or is there another way to get this test to pass?

    Thanks.