Ashley Moran
2007-Feb-12 14:43 UTC
[rspec-users] Specs for Ajax partials with unicode characters
Hi Not sure if this is more Rails or RSpec related... I''ve got an app with an RJS view that updates a div in a page with the contents of a partial. The partial contains a non-ascii character namely a pound sign. I set up a simple test app with this RJS view: page.replace_html("test_div", :partial => "test_action") and this _test_action.rhtml: <p>?500</p> Now this spec fails: require File.dirname(__FILE__) + ''/../../spec_helper'' context "Given a request to render view_test/test_action" do setup do render ''view_test/test_action'' end specify "the response should have a p with ?500" do response.should_have_tag ''p'', :content => ''?500'' end specify "the response should definitely have a p with ?500" do response.should_have ''p'', :text => ''?500'' end end The error for the should_have_tag gives this error message: Element.update("test_div", "<p>\u00a3500</p>"); should include ["[\"p\", {:content=>\"?500\"}]"] I don''t know, well, anything about JavaScript but I''m informed by one of our developers that the replacement string is in JSON format. Either way the only way to make the specs pass is to use :content (or :text) => ''\u00a3500'' which is completely different from the contents of the partial, so not a brilliant solution. An alternative is to use £ for ?, but that kinda defeats the point of using UTF8. Any ideas how I can get round this? Cheers Ashley
David Chelimsky
2007-Feb-12 15:13 UTC
[rspec-users] Specs for Ajax partials with unicode characters
On 2/12/07, Ashley Moran <work at ashleymoran.me.uk> wrote:> Hi > > Not sure if this is more Rails or RSpec related... > > I''ve got an app with an RJS view that updates a div in a page with > the contents of a partial. The partial contains a non-ascii > character namely a pound sign. > > I set up a simple test app with this RJS view: > page.replace_html("test_div", :partial => "test_action") > > and this _test_action.rhtml: > <p>?500</p> > > Now this spec fails: > require File.dirname(__FILE__) + ''/../../spec_helper'' > > context "Given a request to render view_test/test_action" do > setup do > render ''view_test/test_action'' > end > > specify "the response should have a p with ?500" do > response.should_have_tag ''p'', :content => ''?500'' > end > > specify "the response should definitely have a p with ?500" do > response.should_have ''p'', :text => ''?500'' > end > end > > The error for the should_have_tag gives this error message: > Element.update("test_div", "<p>\u00a3500</p>"); should include > ["[\"p\", {:content=>\"?500\"}]"]Ashley - are both specs failing? Or just the one using "should_have_tag"?> > I don''t know, well, anything about JavaScript but I''m informed by one > of our developers that the replacement string is in JSON format. > Either way the only way to make the specs pass is to use :content > (or :text) => ''\u00a3500'' which is completely different from the > contents of the partial, so not a brilliant solution. An alternative > is to use £ for ?, but that kinda defeats the point of using UTF8. > > Any ideas how I can get round this? > > Cheers > Ashley > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Ashley Moran
2007-Feb-12 16:30 UTC
[rspec-users] Specs for Ajax partials with unicode characters
On 12 Feb 2007, at 15:13, David Chelimsky wrote:> Ashley - are both specs failing? Or just the one using > "should_have_tag"?David Both specs fail. The text mangling must happen before it reaches RSpec, so that both assertions see the same encoded string. Ashley
David Chelimsky
2007-Feb-15 16:49 UTC
[rspec-users] Specs for Ajax partials with unicode characters
On 2/12/07, Ashley Moran <work at ashleymoran.me.uk> wrote:> > On 12 Feb 2007, at 15:13, David Chelimsky wrote: > > > Ashley - are both specs failing? Or just the one using > > "should_have_tag"? > > > David > > Both specs fail. The text mangling must happen before it reaches > RSpec, so that both assertions see the same encoded string.Yes - looks like json does that transformation. My feeling is that you should probably use ''\u00a3500'' and that''s OK. The reality is that is what shows up in the response. WDYT? David> > Ashley > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Ashley Moran
2007-Feb-15 18:02 UTC
[rspec-users] Specs for Ajax partials with unicode characters
On 15 Feb 2007, at 16:49, David Chelimsky wrote:> Yes - looks like json does that transformation. My feeling is that you > should probably use ''\u00a3500'' and that''s OK. The reality is that is > what shows up in the response. > > WDYT?Eventually I decided to do js_pound_sign = ''\u00a3'' and test for js_pound_sign + ''500'' This works for me. On the one hand, I agree that it makes sense to test the output that is actually rendered. On the other hand, it''s counter-intuitive to have a partial containing "??czno??" (or so my Polish colleague tells me) and test for "\u0142\u0105czno\u015b \u0107". The Rails file activesupport/lib/json/encoders/core.rb only contains a JSON string ENcoder, but I assume the decoding is straightforward enough. What would be nice is something like context "my view" setup do render "moo/quack", :json_to_utf8 => true end ... specs all receive UTF8 strings ... end I don''t know if it''s worth the effort right now though, seeing as you probably can just put dummy text in for most stuff that would need UTF8 characters. Non-English speakers will probably disagree :) Ashley
David Chelimsky
2007-Feb-15 18:31 UTC
[rspec-users] Specs for Ajax partials with unicode characters
On 2/15/07, Ashley Moran <work at ashleymoran.me.uk> wrote:> > On 15 Feb 2007, at 16:49, David Chelimsky wrote: > > > Yes - looks like json does that transformation. My feeling is that you > > should probably use ''\u00a3500'' and that''s OK. The reality is that is > > what shows up in the response. > > > > WDYT? > > > Eventually I decided to do > js_pound_sign = ''\u00a3'' > > and test for > js_pound_sign + ''500'' > > This works for me. On the one hand, I agree that it makes sense to > test the output that is actually rendered. On the other hand, it''s > counter-intuitive to have a partial containing "??czno??" (or so > my Polish colleague tells me) and test for "\u0142\u0105czno\u015b > \u0107". > > The Rails file activesupport/lib/json/encoders/core.rb only contains > a JSON string ENcoder, but I assume the decoding is straightforward > enough. What would be nice is something like > > context "my view" > setup do > render "moo/quack", :json_to_utf8 => true > end > > ... specs all receive UTF8 strings ... > endMy concern w/ this approach is that it adds a new potential point of failure. IF the decoder has a bug you''ll get false pass/fail and it would be difficult to track down. This is something that''s been bugging me about Rails in general lately. Why should it need custom testing tools? That smells funny to me. WDYT?> > I don''t know if it''s worth the effort right now though, seeing as you > probably can just put dummy text in for most stuff that would need > UTF8 characters. Non-English speakers will probably disagree :) > > Ashley > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Ashley Moran
2007-Feb-16 10:30 UTC
[rspec-users] Specs for Ajax partials with unicode characters
On Feb 15, 2007, at 6:31 pm, David Chelimsky wrote:> My concern w/ this approach is that it adds a new potential point of > failure. IF the decoder has a bug you''ll get false pass/fail and it > would be difficult to track down. >That''s a good point I hadn''t considered. It seems risky to add a layer of code between the output of Rails and the tests. Unfortunately I don''t know *any* JavaScript - haven''t written a line of it in my life. So I can''t say why the Rails team escapes UTF8. All the other content in my Rails apps gets served as UTF8, so I don''t see why JavaScript should be any different. Maybe it''s a browser workaround. I might ask on the Rails list.> This is something that''s been bugging me about Rails in general > lately. Why should it need custom testing tools? That smells funny to > me. > > WDYT? >Not sure I''m in a position to comment. Like I say, maybe in this case there''s a reason for the bizarre text munging. However, I agree with what you said a while back about ActiveRecord impeding mocking (I can''t say about the other components as I haven''t seen the source). I''ve had a look at the Og code recently, and it seems to have much better separation of concerns. (Having said that, I saw a blog post by the main developer saying he thought it was getting crufty.) Much as I like ActiveRecord, the code is incredibly tightly bound. I did some work at the end of last year to add class-table inheritance to ActiveRecord. I had to hack pretty much all of the Base class code, and it can be really tough to follow the flow through all the methods it contains. Testing changes it tough too because it''s all pretty monolithic. I got to the point with CTI where it was working but without associations. I realised that adding support to the patch for associations would end up with me duplicating virtually all of the unit tests. They are frustrating to work on because the fixtures have become quite brittle. I was about to suggest to the core team that I rewrite all the unit tests in RSpec, which would give an opportunity to audit the tests, probably clean them up a bit, and also give me the opportunity to slot in CTI without turning the unit tests into an unmaintainable copy-and-paste frenzy. (Kinda depends on them wanting CTI though.) I actually tried to port one of the basic tests, but I couldn''t manage to integrate RSpec into the Test::Unit stuff. There might be some opportunities to refactor ActiveRecord a bit, see if the code can''t be made more isolated and testable. When I get 30 continuous seconds free I''ll put all this to the core team. Unfortunately I don''t have a staggering amount of free time to work on it. Ashley