Comments (1)
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)
- Well done HOT 1
- Shared db connection
- Ch.9 Avoid useless stubbing
- Ch.5 assigns[key] is used
- Ch 9. How about refactoring contacts_controller_spec ?
- Ch 9. set_user_session is not used
- Ch 8. Feature spec works without find method
- Ch 6. id is not required for create
- Ch.5 be_true should be be_truthy
- Where to place `gem "factory_girl_rails"` in Gemfile HOT 6
- application.rb p. 17 HOT 2
- Can't pass `bundle exec spec` on 05_controller_basics branch HOT 3
- error with invalid contact for controller spec HOT 3
- CH.5 Failing Test HOT 1
- selenium-webdriver won't work against Firefox 48 or later HOT 2
- [error] newer bundler causes: Your Gemfile.lock is corrupt. The following gem is missing from the DEPENDENCIES section: 'rspec' HOT 1
- Chapter 5 Testing Controllers HOT 1
- Question on controller testing advanced..authentication comment out... HOT 1
- ch. 5 pag. 68 populates an array of all contacts (FAILED - 1) HOT 6
- 09_speedup "ruby_dep requires Ruby version >= 2.2.5, ~> 2.2." HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rails-4-1-rspec-3-0.