-
Notifications
You must be signed in to change notification settings - Fork 0
Testing
Noah Rijkaard edited this page Aug 20, 2024
·
3 revisions
The ShortenedUrls controller was thoroughly tested to ensure all routes and actions behave as expected. Here's a breakdown:
-
Index Action:
- Test: Verifies that a new ShortenedUrl object is assigned to @url and the index template is rendered.
- Purpose: Ensures the view correctly receives a new instance of ShortenedUrl and that the user is presented with the proper template.
-
Show Action (Redirect):
- Test: Confirms that accessing a shortened URL (GET /:short_url) redirects to the sanitized target URL.
- Purpose: Ensures the redirection functionality is working correctly, which is a critical feature of the URL shortener.
-
Create Action:
- Valid Parameters:
- Test: Checks that a new ShortenedUrl is created and the user is redirected to the shortened URL.
- Purpose: Validates the core functionality of generating a new shortened URL.
- Invalid Parameters:
- Test: Ensures no ShortenedUrl is created when the input is invalid, and the user is returned to the index template with error messages.
- Purpose: Validates the application's robustness by ensuring it handles invalid data gracefully.
- Duplicate URL:
- Test: Confirms that a duplicate URL does not create a new record but instead redirects to the existing shortened URL.
- Purpose: Prevents the database from storing redundant data and enhances user experience by reusing existing shortened URLs.
Testing the ShortenedUrl and UserStat models involved validating the core business logic and geolocation features:
-
ShortenedUrl Model:
- Test: Verifies that the unique short URL generation works correctly, avoiding infinite loops by checking for duplicates.
- Sanitization:
- Test: Ensures the URL sanitization process correctly strips unnecessary parts of the URL, standardizing it.
- Purpose: Critical for ensuring consistent data storage and preventing errors during redirection.
-
Title Retrieval:
- Test: Automatically extracts and capitalizes the domain name as the title of the shortened URL.
- Purpose: Enhances user experience by providing a recognizable name for each shortened URL.
-
UserStat Model:
- Geocoding:
- Test: Confirms that the model correctly geocodes the address into latitude and longitude upon validation.
- Purpose: Ensures accurate location tracking for each user interaction.
- Timezone Assignment:
- Test: Verifies that the appropriate timezone is assigned based on geolocation data.
- Purpose: Critical for accurate tracking and analytics.
- Validations:
- Test: Ensures that the model cannot be saved without essential attributes like origin_country.
- Purpose: Guarantees data integrity within the application.
- Geocoding:
Tailwind Configuration in Testing: The need to invoke the TailwindCSS build task before running tests ensured that all assets were correctly compiled. This was a necessary step to avoid issues with view rendering during request specs.
- Geo-stubbing: The use of Geocoder.configure(lookup: :test) allowed for stubbing the geolocation data during tests. This was critical for testing the UserStat model without relying on external APIs, which could be unreliable or lead to inconsistent results.