Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7178269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:53:13+00:00 2026-05-28T16:53:13+00:00

I have a model that requires a valid format of a URL. class Event

  • 0

I have a model that requires a valid format of a URL.

class Event < ActiveRecord::Base

  validates_format_of :url, :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix

end

BUT BEFORE implementing the solution I want to write a test that fails. Is this how someone would go about writing a failed test? (Please no Rspec or Shoulda solution trying to stick with basics before going to advanced testing / matcher frameworks.

class EventTest < ActiveSupport::TestCase

setup do
# These attributes are valid
@event_attributes = { :title => "A title",
                      :url => "http://somedomain.com/images/land.jpg",}
end

test "should not be valid with an INVALID URL" do
    @event = Event.new(@event_attributes.merge(:url => "htp:/domain"))
    assert_no_match(/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix, @event.url, "Not a valid format")   
  end

end

Is the assert_no_match the right approach. Any suggestions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:53:14+00:00Added an answer on May 28, 2026 at 4:53 pm

    It’s not the right approach, because you’re not testing Event validation at all. Instead, a common pattern is to create an object with an attribute that will trigger the validation error and assert that the validation exception has been raised. So, in your case:

    assert_raise(ActiveRecord::RecordInvalid) do
      @event = Event.create!(@event_attributes.merge(:url => "htp:/domain"))
    end
    

    Rspec is good, so you should try it at some point, and better sooner than later, I think. Also there is a nice library for the @event_attributes.merge pattern you’ve used: it’s called factory_girl. Check it out, it will save you some trouble.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user model that requires the user to change their password every
I have a standard active record model with an attributes that is required: class
I have a model that is something like this: class Input(models.Model): details = models.CharField(max_length=1000)
I have a class that I am using to model my data in MVC.
I have model that looks like this: public class Lesson { public int Id
I have a Family class so defined: class Family < ActiveRecord::Base after_initialize :initialize_family belongs_to
I have a class that requires another class to be specified, but I don't
I have a model that looks like this: class Movie(models.Model): title = models.CharField(max_length=200) slug
I have a valid JSON String that I want to tidy/format such that each
I've got the following User model: class User < ActiveRecord::Base # Users table has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.