Bestand wählen

Understanding Rails test types in RSpec

Zitierlink des Filmsegments
Embed Code

Automatisierte Medienanalyse

Erkannte Entitäten
and the and to introduce myself my name is something where you or I have fans ahead and some 15 on Twitter and some on get hold and you have a look at my propose there if you are interested in finding out more about me if you do look at my job profile you find I spend most of my time on gap of committing as a member of the Auspex scene and that's really why I'm here today because I think it's really important that all spec is well represented in community events like this and everyone who uses the framework has the ability to talk to someone who works on it on a regular basis and can ask questions make comments and that sort of thing so if you see me walking around a conference at all throughout the week please just come and grab me and we can talk about us back and we remain office hours at the Harvard Q community brief on Wednesday during the happy hour so you can come and find me there as well I work for a company called fun and plausible solutions were consulting agency that specializes in data science and refactoring slash testing big complicated rails lots of if you're toll interested in working together on that sort of thing please come have a chat with me afterward I prefix all of my
talks with the following statement which is I think it's more interesting for us to have a discussion on the need to necessarily get to the end of my pre-planned content so I would encourage you to pop your hand up and ask me questions as I'm talking if you're not quite following what I'm saying or explaining something badly it can be really helpful to ask a question that both enables me to clarify my thoughts and also helps everyone understand if you have a question it's very likely that somebody else in the room well yeah as well and I wanted to start this talk by providing 1 of many possible motivations for why we actually writes Automated Test straw applications like a lot of people who are beginner developers have been taught that you must test you applications but oftentimes the reasons that a given off flimsy or easy to you argue with not saying mine isn't either but here's 1 idea that I have that we can sort of talk about so my idea is the comments in life it's
not that when you're writing your application you write a comment that you're doing a disservice to the other developers on your team your trying to help them understand the software that right unfortunately during like high-pressure sudden deadlines and changes having the code really quickly it to be very easy to leave comments Elidan as they were at as we make changes for applications the purpose of
covered in replication changes over time and when the purpose changes it may be that the label that you pull on it no longer applies when your brain is
reading very hairy piece of code you also scrambling to find any anchor on to which you can graph to help you understand what's going on the and there's nothing worse than a confusing or poorly written comment which causes you colored assumptions in your brain that may then be dashed comments that contain
short numbers of words that don't necessarily have all that much meaning can be incredibly frustrating when you come to a new project the words that the people that
maintain the project all using me before and to you it may cause you to fail to understand what's going on
test don't like test provide an objective truth about system that they're executing all tests all tree period when you run them that poking at a system and providing new answers about what the system does that's not to say that you can't write bad test test and just as confusing as bad comments but they don't like tests allow you to roll what knowledge that you have about new applications into something that is literally executable and communicate with your team vibe form of executable test because let's talk
about rails there were some pretty big announcements about changes in rails 5 that we're gonna be happening this morning unfortunately I haven't been able to update everything that I was going to say about rails for the new 5 changes but
suffice it to say that I still think it's true that rails raises the barrier to testing more than necessary code in would help you write your applications specifically 1 of the reasons why I find beginners struggle with testing rails applications is the rails tests can take many different forms and those tests have different purposes for testing different parts of the system it can be difficult to hold all of those test types in your brain and understand what's going on let's
talk about the rails test types that I think people most commonly test the rails applications with 3 that we're
going to talking about state model tests controller tests the feature tests these 3 groups of tests allow you to cover most of the behavior of your application when begin developing it and I certainly find that when I'm working on rails applications a richer 1 of these 3 test types before I reach for any of the others they're going to be talking about something called outside
in test and this is an idiomatic pattern from writing tests in your application that helps you structure the tests that you write and also to better understand the code you going to be testing as you write the idea with outside and testing is that you start by writing a test at the very highest levels describing a replication in times the user or browser on HTTP client men might your application which we gets treated as a black box you don't look inside it you don't look inside the database you don't look inside any part of your application you exercise your Rails as a user might by yourself on the behavior of the user would see when you do you want once you got outside tests that you are happy with the outside in paradigm that encourages you to highlight test the literally inside replication testing individual controllers for individual models the idea behind this is that you end up with the sort of Tulane test structure 1 that describes your application that in a very sort of user-centric way and the other in recovered centric way when those combined you get really useful behavior verification a lot of communication about where application bells and also it really helps you design and how the application works and to explain the theory but outside in
testing i'm gonna do about 20 minutes of life coding and it as well we should gain an understanding of what the rails test types or and I finish up and take some questions it so to
switch over to my terminal here we should be all
good anaphora run arousal and especially you a Rails application but I've built here that's going to be the application that we're going to be testing today this is your basic 101 rails to do list applications it has a single model a single controller to to use a very simple models and they have a flag that says whether or not that Don the text and note which allows us to describe what we actually want to do I can set the 2 due to Don by using the rails admin consul when I get back to my application you can see that changes be made so let's imagine the all customer has come to us and asked us to seperate this lists out into 2 different lists 1 wanted 2 the complete and wanted to use the incomplete and we've decided that we're going to make this change in extremely test-driven way switching over to my terminal him we can see that our my tests I have a single feeling future test and defeated test is saying I expected to find this header for the completed tedious but which I didn't find to go into the test filed let's
as she talk about what this test is doing so this is a Rails feature test feature tests but most commonly used to do the outside part of the outside and testing cycle I was talking about this test is literally only manipulating a web browser and expecting on the results of a web browser sees it doesn't know about model where all the controller layer or anything in the application to write an aspect feature test you at this declaration when you do the described this as type of feature and what that does is it causes all spec to include a number of methods into a test that allow you to actually manipulate the fake browser the OS make users to drive rails applications the rest of this test is structured in a fairly interesting ways and the reason is that the test is not describing out what it's doing in terms of like simple alspac methods instead I've got 2 methods here could visit home page and I should see the completed titties had which are the tree just methods that coal straight in to you all spec close the reason I've done this is that I really like to write feature tests in such a way that you could imagine a user describing your application in terms of the words that you have in your feature test you can see here that we say this at home page emergency the completed tutors had these methods are actually implemented inside this module called to do steps and just go into that we can see that to do steps is just a collection of methods that actually call into Auspex fake browser and Auspex expectations try to write tests on the home page but just as visits slash as you might expect and then I should see the completed tutors header says expect page have content completed to so I could have just written both of those lines of courage In this test but there are a couple of advantages gained by not doing firstly it means I can reuse this behavior in other tests and secondly as I mentioned earlier it means the test is described in terms of user language In order to make the test pass we need to satisfy this expectation for the page has content completed tutees
and to do that at all I need to do is go into the index html body of the page and add a header at the bottom which says completed changes at this point if I run my test it should
pass the and it that's going back to
the actual Rails application in a browser we can see that head has now been added now that's not very interesting behavior change but what we've done here is we like written a 1st test the gives us it's actually be allowed to continue to write more tests the application and now we're going to do is we're going to write a test which creates a completed to expected to be in a separate list so you to do that I'm going to go back into my test
and then to add a context book but in alspac context looks just allow you to describe different states of the application in this case the stages with a completed treaty in order to build my completed to you before my test visits the to form and I'm going to create a complete to do with note by and that means that for each test inside this context book was actions will be wrong and you will be created the behavior of I want is it shows the completed in the completed to use list and then all I need to do now is go back to the home page to make sure I'm seeing the table and then say I should
see they completed 2 you would not
buy milk at this point we have a test for next piece behavior that you want which is actually listing out all of the to use in the applications the well run this test it
should fail and the reason that it fails is it says it can't find CSS hash completing use what this by should see a completed 2 you would know is doing is is looking for an element on the page that has the HMO Aidid completed to use and they're looking at the content within that table all data
due to make this test passed is copy the table from above pasted down to get the idea uh typing is food and running
the test kitchen now pass it does so let's look at the TD spec that we've written
so far what not to spike given so
far we've added ahead of and we've got the separate table that we've created with an idea that lists are completely but we're not quite done yet the reason for that is that writing tests is necessarily adversarial and all I got here is the behavior when there is only 1 complete to do in the database was still listing all of the to use in the but new table so a new contacts which is and with an incomplete T and similarly register copy you most of this before book and what is the change complete to incomplete without by x and then all test will be it was not sure the completed of the incompletes to do in the completed to table and then all need to do his homepage I had this happen page by the should not
see uh completed to do would know
by eggs as a now we have opposite expectations right we have an expectation the completed 1 shows up in the table and the incomplete 1 doesn't running this test should
fail calls like all type the someone help the when the nature and right and so now we've got the
by eggs in our table of complete notes when we shouldn't and so at
this point the only way we can make that actually work is to change the way that the instance variables in the
controller get passed through to the next item elderly of the you can see here that active users being used for completed to use table but as we know from our just like basic rail scaffolding implementation that's is gonna be to you don't all so if we hadn't gotten into the 2 you control of we can see here but that is indeed
to the behavior answered before we make a change this controller let's write a test other calls it have the behavior that we want and so at this point I'm just
going to actually write a test for the control of only now we are insights that about outside in testing cycle we know the testing the application as a whole but instead focusing on a single control and because
this is a controller test I need to add the tight control of uh symbols to this test and just like a feature test the control test label type controller has methods included into it that are uh for testing control we're going to describe get index here which is the action that we need to add are instance variable to will say it returns completed not only the completed candidates and what do you know to get set up is urging the right uh create some models in the database directly directly so here I say to you don't create Don true but note by look and that let's will give us a instance variable of sorry a method in all of all tests who completed 2 2 rows are gonna create an incomplete 1 which has done such false and different notes and then before all test but you can create both of these i'm going to fall off specters not evaluate let's until they're referenced
inside a test and what we're out you can have a problem with here is we're going to make the request what controller and then expect to see results in order for that work we need to put the items in the database before we actually run a test but I don't wanna say is get index and expect the science of complete to do so to that array
containing completed to you and let's talk about what this test the same aspect control tests do not actually create HTTP requests and by default they do not render views that means that the interface control test is the instance variables are assigned during the execution of your control of an Austek makes that available while this assigns methods which takes a symbol which will then get reference to the instance variables get indexed those more or less exactly what you'd expect it sends the index method to the controller with a fatal rails http request that has the get methods 1 running this test it's going to fail because we haven't assigned the complete tutees instance variable so by default off spec gives that the value of
now now we have a failing test we can go back
into the control of which actually implement all
behavior and must say complete to do this equals to your where Don true which is why I believe to be the behavior
necessary to make the test pass it does another run all tests again will be reminded which test need to satisfy next assess testimony to satisfy next is off feature test for the 2 back which we can satisfy
just by changing the index later be to reference completed use running all of our
tests now we should be completely agree so at this point we've implemented the feature of our customer calls for we separated out the completed tutees into their own list at this point but it would be perfectly fine for like stop writing software and put the rails up down but there's a small smell him but I think we
can do better with its to do to use control
if you look at how index is actually written the index method is at 2 different levels of abstraction when it's 2 different life it's activities equals 2 don't all is a pretty high level description of what we're doing the next line down a complete use equals 2 you don't want true knows much more about the database it knows that as a Don color and that when it's true that means the you use complete I think it would be better to write something like this but in order to do that we need to add a method to all models and those do
that we should really write a test for a model before we do that so let's do it we're is going to do this and then like we did for control test this test will be describing the model class and will have type model that's set on just like tight control on typed feature type model gives you off but methods in your test for testing models we're going to describe a class method on a to you which is called complete to use and the behavior of this method is in the very same a very similar to or to control a spec if you look
retching creating a completed and in completed to you in all tested as elitist copulas informal test and then write a very similar task force as it returns only the completed students and I'll just be expect to you don't complete to do is to that of complete you and the reason that we create both is again so that we can be sure that that is the behavior that's really happening if we run this test is going to fail because the method is undefined knowing about into all
to do model and just added onto the class with self come pleats to this is where Don true this point if you run all of all tests they should all Paul's
find the odds of this needs and deal on the end of it the if I they do and knowing about into what you
controller and we can drop that methadone the work to the completed 2 days than other lot has we should be great and this is an example of a really simple refactoring that you can do that whenever you were doing this like test of what we see an active Record scope in a controller is often a good idea to push it down into the model so we now implemented or
change we can related or rails up here and we can see on the the 1 that has done tree implement implemented is in the lead to table and that concludes the life coding
portion all of this book
I'm sorry I wanted to finish up with a couple of uh closing
remarks the first one is that is it is not actually a natural result of just like the laws of physics there would be possible to test rails applications with all spec a lot of time and a lot of effort has gone into making it possible To test rails applications with all spec and the thing about rails is like it's a bit of a shifting platform as we heard this morning during a bunch of quite strange new features and really that we as a community have to people to you mainly thank you for abilities test rails applications in all spec most people are and promote and
handling them so have a long time ago when rules 3 came out and he was like let's test rooms obligations would lost and he put like a ton of effort into maintaining the rails jam over a number of years and he has now stepped away from working on all spectacles he's moved on to other things because Nauru chain that because open source is hot like working on it is difficult so when and the stepped away in this weird position where like actually nobody on the Auspex what he was working on rails applications of which is a slightly weird position and then this guy I'm came along was like hey let's do this and so on is now sort of leading the charge on maintaining rails and actually and is in the room and we use the bigram applause fewer there so as as you made tell I'm not from here
should that I I came over on Sunday from London uh the flight is pretty pleasant 9 hours I'm dead and ill so that's great but I really love coming to America I always have a great time when I'm here and as any true bread who loves America I carry really take 1 starts in this country I came here to take that we call
it and I think I've worked out how I'm going to do it this time because you see america
the national animal of your country is an eagle and another pretty cool don't get me wrong but the
crest of the British Empire has a Lion and the Unicorn and I'm pretty sure the lion and unicorn could take a needle in a fight any day of the week this is not my 1st time in Atlanta i've
been here a number of times full and obviously the barbecue
here is fantastic I can't recommend it enough but there's 1 thing here is absolutely terrible
sweetie non 0 my god what are you doing I case
like sugar and you have the less I hate it it's so wrong she is supposed
to look like this milky and Brown and delicious and recovering thank you very
much I have here the eliciting the
reality of you and you will live within some wounds um of
Gewichtete Summe
Schreiben <Datenverarbeitung>
Gesetz <Physik>
Deskriptive Statistik
Fahne <Mathematik>
Radikal <Mathematik>
Befehl <Informatik>
Vervollständigung <Mathematik>
Profil <Aerodynamik>
Kontextbezogenes System
Quelle <Physik>
Einheit <Mathematik>
Rechter Winkel
Grundsätze ordnungsmäßiger Datenverarbeitung
Ordnung <Mathematik>
Software Engineering
Tabelle <Informatik>
Klasse <Mathematik>
Demoszene <Programmierung>
Fächer <Mathematik>
Inhalt <Mathematik>
Open Source
Nabel <Mathematik>
Wort <Informatik>
Natürliche Zahl
Formale Sprache
Kartesische Koordinaten
Element <Mathematik>
Prozess <Informatik>
LASER <Mikrocomputer>
Arithmetischer Ausdruck
Gesetz <Physik>
Arithmetisches Mittel
Verkettung <Informatik>
Twitter <Softwareplattform>
Automatische Indexierung
Deklarative Programmiersprache
Projektive Ebene
Kontextbezogenes System
Framework <Informatik>
Physikalische Theorie
Data Encryption Standard
Elektronische Publikation
Einfache Genauigkeit
Kopula <Mathematik>
Physikalisches System
Visuelles System
Objekt <Kategorie>
Dreiecksfreier Graph
Innerer Punkt
Brennen <Datenverarbeitung>


Formale Metadaten

Titel Understanding Rails test types in RSpec
Serientitel RailsConf 2015
Teil 20
Anzahl der Teile 94
Autor Phippen, Sam
Lizenz CC-Namensnennung - Weitergabe unter gleichen Bedingungen 3.0 Unported:
Sie dürfen das Werk bzw. den Inhalt zu jedem legalen und nicht-kommerziellen Zweck nutzen, verändern und in unveränderter oder veränderter Form vervielfältigen, verbreiten und öffentlich zugänglich machen, sofern Sie den Namen des Autors/Rechteinhabers in der von ihm festgelegten Weise nennen und das Werk bzw. diesen Inhalt auch in veränderter Form nur unter den Bedingungen dieser Lizenz weitergeben.
DOI 10.5446/30714
Herausgeber Confreaks, LLC
Erscheinungsjahr 2015
Sprache Englisch

Inhaltliche Metadaten

Fachgebiet Informatik
Abstract Getting started with testing Rails applications can be a frought process. There are a range of different test types that one can write. It's often not clear which type one wants. Without care your tests can begin testing the same behaviour. This is problematic. In this talk we'll cover the most common types of test you'll encounter in your Rails applications: feature, controller and model. We'll also talk about ways you can design your tests to ensure your suite is robust to changes in your system. If you'd love to learn more about RSpec, Rails, and testing this talk will be great for you.

Ähnliche Filme