在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:learn-co-students/javascript-logging-lab-js-intro-000开源软件地址:https://github.com/learn-co-students/javascript-logging-lab-js-intro-000开源编程语言:JavaScript 100.0%开源软件介绍:JavaScript Logging LabObjectives
IntroductionWelcome to your first JavaScript lab! You'll notice a few new things in this lesson that we haven't encountered before. Don't worry, we'll walk you through them. Tests...The first new thing you'll notice is tests. When we want to run an experiment, we need to develop a hypothesis and we need to test it. So if we want to experiment with whether adding salt to ice water makes it hotter or colder, we need to design an experiment that controls for all of the other variables: we need to isolate our experiment from parts of its environment that aren't relevant to what we hope to test. In programming, tests place the scientific method into computer science. We run tests to verify that our programs behave the way we think they do. Tests help us identify bugs, and they give us a sense of the health of our applications. On Learn, we use tests as teaching tools. Just like in a normal coding environment, we use tests to describe the program's behavior. Unlike in a normal coding environment, you, not we, are in charge of getting the tests to pass — that is, making the app behave like we expect it to. StructureThe structure of this lab — where its files and folders are located — looks roughly like the following:
All labs will more or less have the same structure. (And READMEs, for that matter, will still have CONTRIBUTING.md, LICENSE.md, and README.md files.)
Code-alongFor now, open up your IDE should open up. You'll see a sidebar like this: If you open up that "javascript-logging-lab..." folder, you'll see a list of files (along with a test/ directory). Click In Now open up Note: The At the very top of the file, you'll see const expect = require('expect')
const fs = require('fs')
const jsdom = require('jsdom')
const path = require('path') This might be a bit bewildering, but at this point, we don't need to be able to write any of this code, or even understand every line perfectly. All we need is to understand enough so that we can get a sense of what the test is asking us to accomplish, so that we can make the test pass. Let's go through it. In these first lines, all we're doing is referencing different libraries that help us run your tests. A library is code that someone else (usually multiple someone elses) wrote for our use. Note that A little farther down the page, you'll see: describe('index', () => {
// there's stuff in here, too
})
Then we have a few chunks like it('calls console.error()', () => {
// this is where the tests are!
}) Each of these chunks describes a behavior that we expect the main program to implement. As you can see, they describe that behavior pretty carefully — in this example, we know that our main file should call Don't worry too much yet about what's happening inside these chunks. Sometimes we'll need to do some pretty fancy footwork to test some pretty basic things; other times, and as time goes on, you'll be able to read and understand basically what our tests are expecting. And that'll be great! These aren't like tests that we all took in school: they're testing behavior, not information. Tests are meant to be as transparent as possible about what they're doing, and as you grow as a programmer, it's important to understand more and more what the aims of tests are. In some of our tests, you'll see lines like the following: jsdom({
src: fs.readFileSync(path.resolve(__dirname, '..', 'index.js'), 'utf-8')
}) This line reads Running the TestsTo run the tests, simply type For the moment, all of the tests fail. Let's figure out how to get one of them passing! (The rest will be up to you.) Let's take the first one. The test description says, "index calls console.error()". So it sounds like, pretty straight-forwardly, like we should call In console.error("HALP!") Because it seems sufficiently dire. Remember to save your file. Anyway, let's run the tests again. In the Learn IDE's terminal, run learn test We should now see: Nice! We got the first one to pass! A note about spiesYou might often see errors like the ones above: We'll try to rewrite these error messages when possible to be more descriptive about what kinds of calls we expected; but know that sometimes, especially later on, we leave the errors intentionally ambiguous for you to work out. Your turnNow it's your turn — can you follow a flow similar to the one we followed together above to get the remaining two tests to pass? Imagine that you're building the user interface for a fancy ATM machine. Because the developers are hip with the latest trends, they're using JavaScript for the user-facing parts. We need a way to send messages to the user: some messages are just updates, some are warnings (the user should not continue doing what they just did), and some are errors (something broke, and we need to recover). Your job is to identify a way of sending each kind of message. Hint: in
JavaScript, you'll probably find ways of telling users things with And again, remember to save your files before you re-run your tests. When all of your tests pass, be sure to run Feeling stuck?In the above, when we ran our tests and saw the message "index calls console.error()", we wrote, console.error("HALP!") Now when we run the tests again and see "index calls console.log()", we should
look at what is the same and what is different between this message and the
previous one. It looks like they're basically the same except for one tells
us to call console.log("I would be a logger.") // get it? ...we're now calling ResourcesView JavaScript Logging Lab on Learn.co and start learning to code for free. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论