I'm using Test::Unit, and I'd like to test a paperclip url.
Right now, I have a test that looks like this:
def test_our_custom_url
dummy = Dummy.new(:image => File.open("#{RAILS_ROOT}/test/fixtures/12k.png"))
dummy.save!
assert_match "/system/images/1/original/12k.png", dummy.image.url
dummy.reload
assert_match "/system/images/1/original/12k.png", dummy.image.url
end
And it fails like this:
Expected /\/system\/images\/1\/original\/12k.png/ to match "/system/dummies/images/000/000/001/original/12k.png?1352140343".
How can I get the ?1352140343 part added to the test?
I see in paperclips source code that they seem to define it like this:
def timestamp_as_needed(url, options)
if options[:timestamp] && timestamp_possible?
delimiter_char = url.match(/\?.+=/) ? '&' : '?'
"#{url}#{delimiter_char}#{#attachment.updated_at.to_s}"
else
url
end
end
but doing dummy.updated_at.to_s returns a normal date/timestamp, not 1352140343 (in this case).
I'm using local storage, if it makes a difference.
Its not good to compare the ?1352140343 part of the url.
you can compare your received_url.starts_with?(expected_url) to check only that part of the strings...
Related
I have a Model user with the following method:
def number_with_hyphen
number&.insert(8, "-")
end
When I run it several times in my tests I get the following output:
users(:default).number_with_hyphen
"340909-1234"
(byebug) users(:default).number_with_hyphen
"340909--1234"
(byebug) users(:default).number_with_hyphen
"340909---1234"
(byebug) users(:default).number_with_hyphen
"340909----1234"
It changes the number ?Here are the docs https://apidock.com/ruby/v1_9_3_392/String/insert
When I restructure my method to:
def number_with_hyphen
"#{number}".insert(8, "-") if number
end
If works like expected. The output stays the same!
How would you structure the code, how would you perform the insert?
which method should I use instead. Thanks
If you're using the insert method, which in the documentation explicitly states "modifies str", then you will need to avoid doing this twice, rendering it idempotent, or use another method that doesn't mangle data.
One way is a simple regular expression to extract the components you're interested in, ignoring any dash already present:
def number_with_hyphen
if (m = number.match(/\A(\d{8})\-?(\d+)\z/))
[ m[1], m[2] ].join('-')
else
number
end
end
That ends up being really safe. If modified to accept an argument, you can test this:
number = '123456781234'
number_with_hyphen(number)
# => "12345678-1234"
number
# => "123456781234"
number_with_hyphen(number_with_hyphen(number))
# => "12345678-1234"
number_with_hyphen('1234')
# => "1234"
Calling it twice doesn't mangle anything, and any non-conforming data is sent through as-is.
Do a clone of the string:
"#{number}".clone.insert(8, '-')
I have a model and a method inside that defined as follows :
class Farm < ActiveRecord::Base
def flags
#has other code which returns a flag_details hash eg:
# flag_details['flag1'] = true;
# flag_details['flag2'] = false;
end
end
Now I need to write a test to verify that a particular div is displayed based on the flags that are set/unset. And I want to write a stub to be able to return these flags and then test if the page is displaying the correct div. How do I correct the following code, to get what I intend:
scenario "display div named flower when flag1 or flag2 is false" do
farm.stub(:flags).and_return("flag1" => false, "flag2" => false)
if !farm.flags['flag1'] || !farm.flags['flag2']
expect(page).to have_selector('div.flower', text: "4" )
end
end
I am a beginner in ruby, rails and rspec, so any help will be great. I also tried using the following method but it did not work:
farm.stub(:flags[]).with('flag1').and_return(false)
farm.stub(:flags[]).with('flag2').and_return(false)
I also checked this documentation (https://www.relishapp.com/rspec/rspec-mocks/v/2-4/docs/method-stubs) but did not get my answer. Any other links that could be helpful in this, are really appreciated!
Thanks!
I think you should just be able to wrap that return value in curly braces ({}) and it should solve your problem:
farm.stub(:flags).and_return({"flag1" => false, "flag2" => false})
First, get rid of the if conditional in your test. There is no reason to add control flow to a test. How stubbing works: Your test needs to call the flags method on that farm object and it will return the hash that you have specified. Have your scenario visit the page in question and then check to make sure the expectation is behaving as you have intended.
I'm a newbie to Capybara. I'm using the WebDriver driver and I would like to append to a field value as opposed to overwrite the entire contents.
When I use the method fill_in, it overwrites the whole value (clears and then populates). However I want to just append to an existing value in the field.
Looks like send_keys will be your friend.
Try find_field('Your field').native.send_keys('some text')
May be you would like to do it in this way specifically in Capybara
def anyMethod
locator_strategy = :css
if(obj_locator[0]=='/' || obj_locator[0,2] == '(/'))
locator_strategy = :xpath
end
find(locator_strategy, locator_path).native.send_keys('some text')
end
In Grails, if I define a locale, and put a date on specific format on i18n file, like (dd/mm/AAAA), if call one request like:
http://myapp/myaction?object.date=10/12/2013
When I get print: params.date, it comes to me a date object.
How can I do the same on rails?
Normally the Rails handles this for you. For instance, the form helper datetime_select works in conjunction with some activerecord magic
to ensure ensure time/date types survive the round-trip. There are various alternatives to the standard date-pickers.
If this doesn't work for you e.g. rails isn't generating the forms, there are (at least) a couple of options.
One option, slightly evi, is to monkey-patch HashWithIndifferentAccess (used by request params) to do type conversions based on the key name. It could look something like:
module AddTypedKeys
def [](key)
key?(key) ? super : find_candidate(key.to_s)
end
private
# look for key with a type extension
def find_candidate(key)
keys.each do |k|
name, type = k.split('.', 2)
return typify_param(self[k], type) if name == key
end
nil
end
def typify_param(value, type)
case type
when 'date'
value.to_date rescue nil
else
value
end
end
end
HashWithIndifferentAccess.send(:include, AddTypedKeys)
This will extend params[] in the way you describe. To use it within rais, you can drop it into an initialiser, eg confg/initializers/typed_params.rb
To see it working, you can test with
params = HashWithIndifferentAccess.new({'a' => 'hello', 'b.date' => '10/1/2013', 'c.date' => 'bob'})
puts params['b.date'] # returns string
puts params['b'] # returns timestamp
puts params['a'] # returns string
puts params['c'] # nil (invalid date parsed)
However... I'm not sure it's worth the effort, and it will likely not work with Rails 4 / StrongParameters.
A better solution would be using virtual attributes in your models. See this SO post for a really good example using chronic.
There is already created record, like
Company "Life"
How to make this record to the species
сompany-life
I used parameterize, but it turns:
company-quot-life-quot
As I understand, .gsub(""", "") is not suitable for implementation, since to create too large list of exceptions
Is there may be a way to make record in raw format? (to parameterize later)
thanks in advance!
Here is a non-Rails approach:
require 'cgi'
str = 'Company "Life"'
puts CGI.unescape_html(str).gsub(/"/, '').gsub(/\s+/, '-').downcase
# => company-life
And a pure regex solution:
puts str.gsub(/&\w+;/, '').gsub(/\s+/, '-').downcase
# => company-life
And if you are inside Rails(thanks to #nzifnab):
str.gsub(/&\w+;/, '').parameterize
As #meager said, you shouldn't be storing the html-encoded entities in the database to begin with, how did it get in there with "? Theoretically this would work:
class Page < ActiveRecord::Base
before_validation :unescape_entities
private
def unescape_entities
self.name = CGI.unescape_html(name)
end
end
But I'm still curious how name would be getting there in the first place with html entities in it. What's your action/form look like?
"Company "Life"".html_safe.parameterize
"Company "Life"".gsub(/&[^;]+;/, "-").parameterize.downcase
# => "company-life"
Firstly, gsub gets rid of html entities, then parameterize gets rid from all but Ascii alphanumeric (and replaces them with dash), then downcase. Note that "_" will be preserved too, if you don't like them, another gsub('_', '-') is needed.