Chapter 11 - Testing

When testing CategoryTests.swift and AcronymTests.swift, I had to drag them into the project, into the AppTests folder and I added them to the targets: AppTest and TILAppPackageTests, which created an Objective-C bridging header, then all the tests were successful. Was that the correct way of going about it? If I just added them to App target the testing failed.

There should definitely be no Obj-C bridging headers involved! The way to do it is to copy the files to the right directory in terminal and then regenerate the project

I encountered a problem where the test crapped out with the message: “CommandError.unknownCommand: Unknown command ‘revert’”

This was fixed by adding the following lines after “var revertServices = Services.default()”:

    var commandConfig = CommandConfig.default()
    commandConfig.useFluentCommands()
    revertServices.register(commandConfig)
1 Like


oh, I see this was because I skipped over the “deploy to vapor.cloud” section.

1 Like

This isn’t working for me and I have the same issue. But even the lines that you suggest adding don’t fix it for me. Is this only supposed to work on vapor cloud?

@goelankit did you rebuild the application from the command line after adding the lines? It should work locally as well as any other platform

Rebuilding did not fix the issue.

The error is:

error: -[AppTests.UserTests testUsersCanBeRetrievedFromAPI] : failed: caught error: :warning: [CommandError.unknownCommand: Unknown command migrate]

I ran the provided sample project. And the tests ran. So I must have done something wrong.

I’m getting the same “Unknown command ‘migrate’” error.

Thread 1: Fatal error: ‘try!’ expression unexpectedly raised an error: :warning: CommandError: Unknown command migrate
- id: CommandError.unknownCommand

It works if I test using the Final project from the Chapter 11 folder. When I test my project, the debug console shows that all of the migrations are successfully reverted and then all migrations are successfully performed. Then I get the following message:

Usage: vapor < command>

Commands:
  routes  Displays all registered routes.
  revert  Reverts migrations that have been previously prepared.
          By default, only the latest batch of migrations will be reverted.
     boot Boots the application's providers.
    serve (default) Begins serving the app over HTTP.

Use 'vapor <command> [--help,-h]' for more information on a command.

Any ideas? I’ll keep digging!

Thanks.

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

Found my problem!

I had starting building the TIL app with an earlier version of the book. In that version, the “revert” command was added with the following code in configure.swift.

  var commandConfig = CommandConfig.default()
  commandConfig.use(RevertCommand. self , as: "revert")
  services.register(commandConfig)

In the current version of the book, the code in configure.swift looks like this.

var commandConfig = CommandConfig.default()
commandConfig.useFluentCommands()
services.register(commandConfig)

Once I updated my code with useFluentCommands, I got past the “invalid command” message. Now I just need to finish updating my tests to handle the routes that need user authentication.

Thanks for a great book!

Have you managed to get it to work?