Git Product home page Git Product logo

Comments (8)

imajes avatar imajes commented on July 28, 2024 1

i think we can argue that point, i'd say that your lambda and it's chain is way too complex for many developers to quickly scan and grasp the intent.

i understand how you want to restrict magic numbers, and brittle code, but the idea (imho) is that a test should be the simplest expression of intent, even if it's convoluted and non performant -- because this should be as good as documentation for your project.

whilst i understand what your test does, i had to read it carefully to manually unroll it in my head -- and i'm not a compiler.

from betterspecs.

myronmarston avatar myronmarston commented on July 28, 2024

Your example uses the have(n).things matcher, which we've been talking about deprecating. It's probably better to use an example we're not considering deprecating :).

from betterspecs.

imajes avatar imajes commented on July 28, 2024

should == n is pretty straight forward. have(n).things seems overly sacharin and doesn't really add much value.

from betterspecs.

brycemcd avatar brycemcd commented on July 28, 2024

I'd assert that a better practice would be instead:

describe "#save!" do
   context "with invalid data" do
      subject { model = User.new(:email => 'a@invalid.com) }
      lambda do
          subject.save!
          end.should change(User, :count).by(0)
    end
  end
end 

Setting integers in a spec is a code smell to me

from betterspecs.

brycemcd avatar brycemcd commented on July 28, 2024

yeah, I agree that the test should be the simplest expression of intent. There's a lot of value in that. Perhaps the lambda should be written as a custom matcher (or added rspec proper) to read more like:

User.should_change_total_count by(0)

from betterspecs.

warmwaffles avatar warmwaffles commented on July 28, 2024

I think you should utilize the new RSpec expectations. They read much nicer. This is how I have been testing some of my things with ActiveRecord but it's probably not the best solution, but it makes sense.

expect { model.save! }.to raise_error Mongoid::Errors::DocumentNotFound
expect(collection.count).to eq(2)

from betterspecs.

dchelimsky avatar dchelimsky commented on July 28, 2024

+1 to @myronmarston's comment. Elaborating on why ...

collection.size.should == 2
collection.should have(2).items

The latter was added to RSpec back in the day when there was no Cucumber and RSpec was trying to walk the line between readability for tech and non-tech folks. Today I'm pretty convinced that a) nearly no non-tech folk read rspec code, b) some non-tech folk do read the output, but that's all from the docstrings, and c) size.should == 2 is immediately understandable to first-time tech readers, whereas collection.should have(2).items is not.

from betterspecs.

andreareginato avatar andreareginato commented on July 28, 2024

I agree with @myronmarston, thanks for pointing it out. Right now I've just removed the second example and I've left only the lambda/expect one. If you have some more good examples, I'll be to add them.

from betterspecs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.