Code Monkey home page Code Monkey logo

Comments (1)

ruralocity avatar ruralocity commented on June 30, 2024

I see in the controller specs that you test that the values in the assigned variable are the correct ones. How do you proceed when the find method is more complex? For example User.search(various_params), you surely have it well tested in the model specs, so what do you check in the controller? That the search method is called and is passed the params? This in my opinion breaks the behaviour testing as it checks that a specific method is called with that exact params (-> fragile tests). But if you call the method and check the results you actually test it 2 times (in the controller and in the model)

In a real project, I would typically not test this as heavily in the controller if the functionality had been moved to another object. However I like writing these tests for the sake of demonstrating how controller tests work, and practicing writing tests in general. They're also useful if my code is doing something unexpected and I want to test user input without spinning up a full feature test—could be that something is causing different values to be passed to User.search, for example.

You duplicated (using shared specs) the same specs for different roles (guest, user, admin), in theory it is a good thing to do (because you want to make sure that it works as expected for everyone), in your experience how much does it affect the time to run all specs? If many actions are shared I guess the times is 2 or 3 times what it'd have been without them

How much time do your tests take to run all specs (models, controllers, features)? And what do you consider "acceptable" and what too much?

It's going to depend. I'm not the right person to ask about an "acceptable" length of time for tests to run, because I'm more interested in making sure I have reliable test coverage for the important parts of my application. I have test suites that run in under a second. I work on one that take 20+ minutes.

As I mention in the book, yes, you can speed this up considerably by relying on unit tests and mocking out implementation details—but that can get you in trouble if your mock behaves differently than the object or service it's pretending to be. If you're an experience developer, then go for it. If you're a newcomer, focus on writing good tests first, then fast tests later.

I see you suggest shoulda, isn't a big duplication mirroring the real code? I mean, you have a spec that says that it { should validate_presence_of(:arms) }, and then in the model you have validates :arms, presence: true... I'm not 100% convinced about that test

If you're writing the tests after you've written the code, then it's probably not necessary. Shoulda and tests like these in general are useful to me when I am practicing TDD and thinking about how an object I'm creating needs to behave.

I see below that Eric asked about background jobs, and you suggested to test them like you suggest testing rake tasks. What about testing that a job is actually scheduled during another action (in a model or controller)? Would you do something like expect(SomeJob).to receive(:perform_later) or is there a better approach?

Yeah, that seems like a reasonable approach to me.

from rails-4-1-rspec-3-0.

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.