views:

34

answers:

2

I have a big application covered by more than a thousand tests via rspec.

We just made the choice to redirect any page like :

/
/foo
/foo/4/bar/34
...

TO :

/en
/en/foo
/fr/foo/4/bar/34 
....

So I made a before filter in application.rb like so :

if params[:locale].blank?
  headers["Status"] = "301 Moved Permanently"
  redirect_to request.env['REQUEST_URI'].sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{I18n.locale}#{$2}" }
end

It's working great but ... It's breaking a lot of my tests, ex :

  it "should return 404" do
    Video.should_receive(:failed_encodings).and_return([])
    get :last_failed_encoding
    response.status.should == "404 Not Found"
  end

To fix this test, I should do :

    get :last_failed_encoding, :locale => "en"

But ... seriously I don't want to fix all my test one by one ...

I tried to make the locale a default parameter like this :

class ActionController::TestCase
  alias_method(:old_get, :get) unless method_defined?(:old_get)
  def get(path, parameters = {}, headers = nil)
    parameters.merge({:locale => "fr"}) if parameters[:locale].blank?
    old_get(path, parameters, headers)
  end  
end

... but couldnt make this work ... Any idea ??

A: 

Why don't define this locale if not before ?

In your applicationController :

params[:locale] = i18n.locale if params[:locale].blank?

After you application has the local define and futur link could be good.

shingara
If you read until the end you'll see that's not longer a problem I already resolved my locale problem. My issue now is with my tests
Mike
A: 

No offense, but isn't this why global search and replace was invented?

zetetic
search and replace on 300 files and 1250 tests ... I don't think it was invented for more than few replaces ...
Mike