[rspec-users] OT: Implementation Question

Pat Maddox pergesu at gmail.com
Fri Dec 19 11:39:23 EST 2008


On Fri, Dec 19, 2008 at 7:50 AM, James Byrne <lists at ruby-forum.com> wrote:
> I have asked about this issue on several different mailing lists and
> have had no response. If anyone here can offer some advice then I would
> be much obliged.
>
> I have a feature that essentially says:
>
>  Given I have a user "myuser"
>  When I fill in "User Name" with "MyUser"
>    And I press "Login"
>  Then I should find a user "myuser"
>    And I should be logged in
>
> The steps definitions for this are all quite conventional and at the
> moment "Then I should find the user "myuser"" is failing, as expected.
>
> My question is: What is best practice with respect to normalizing form
> input data?  My feeling is that this sort of thing belongs in the model
> but I cannot see a way to get this to work.  In short, how to implement
> this feature?
>
> I have extended String with a method called hll_keycase which preforms
> the desired normalization.  The setter for Users.username is overridden
> in the model and the stored values in the database are all properly
> normalized.  But, it seems to me at the moment that the only place to
> normalize form input data for the SELECT required to determine if the
> username exists or not is either in the form or in the controller.  Is
> that the case?
>
> This has to be a recurring issue for data input forms and yet I can find
> very few resources on the web that address this issue at all, and those
> are often terse to the point of unintelligible.
>
> Not exactly a test issue but I really could use some guidance with this
> as whatever technique I finally adopt will pervade the entire project.
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>

There are a couple things you can do.  First, if you're using webrat
and you've labeled fields exactly as you have them in that feature,
you can offload the work to webrat:

When /^I fill in "(.*)" with "(.*)$/" do |field, value|
  fills_in field, :with => value
end

Or you can do some kind of transformation:

prop_map = {"User name" => :username}
When /^I fill in "(.*)" with "(.*)$/" do |prop_name, value|
  fills_in prop_map[prop_name], :with => value
end

or if you're accessing the model directly
  @user.send(prop_map[prop_name] + '=', value)

Looking back at the full email now though I think you're asking about
how to just convert MyUser to myuser?

class User < ActiveRecord::Base
  def username=(new_username)
    self[:username] = new_username.downcase
  end
end

or non-AR

class User
  attr_reader :username
  def username=(new_username)
    @username = new_username.downcase
  end
end

Is that what you're looking for?

Pat


More information about the rspec-users mailing list