2009-11-23 1 views
0

Travailler sur une demande d'ajouter une jolie info-bulle à une page (en utilisant jQuery tooltip plugin, comme recommandé par d'autres sur mon équipe). infobulle Jolie fonctionne bien, mais deux des spécifications existantes échouent maintenant, un exemple ci-dessous:Comment réparer une spécification pour enlever les balises hml après avoir ajouté javascript à une vue dans Rails?

describe 'with a discussion post containing html' do 

    before(:each) do 
    @post.update_attributes(:body => "some <strong>bold and <i>italic</i></strong> text") 
    assigns[:posts] = @discussion.posts.paginate(:page => 1) 
    render 
    end 

    it 'should strip the html tags from the post body' do 
    response.should say('some bold and italic text') 
    end  
end 

Pour complétude, voici le javascript que j'ai ajouté aux discussions et afficher la page du rspec résultant.

<script type="text/javascript" charset="utf-8"> 
    $(function(){ 
    $('#useful_item_submit').tooltip(); 
    }); 
</script> 

<div id="useful_item_form" > 
    <% form_for [flaggable, flaggable.useful_items.new] do |f| -%> 
    <div> 
     <%= f.submit "I find this useful", :title => 'Click to let others know you found this useful' %> 
    </div> 
    <% end -%> 
</div> 

Dois-je changer le test d'ignorer le javascript supplémentaire ou devrais-je pas le javascript dans le fichier show.html.erb?

sortie de spécification ============

'discussions/show.html.erb with a discussion post containing html should strip the html tags from the post body' FAILED 
<"some bold and italic text"> expected but was 
<"some bold and italic text\n\t\t\n $(function(){\n $('#useful_item_submit').tooltip();\n });">. 
<false> is not true. 

Répondre

0

Je certainement vous mettre javascript dans un fichier javascript.

Je le fais pour faire, y compris javascripts vraiment facile:

en app/views/layouts/application.html.erb quelque part:

<%= yield :javascripts %> 

dans app/helpers/layout_helper.rb:

def javascript(file) 
    content_for(:javascripts) { javascript_include_tag file } 
end 

puis, dans public/javascripts/useful_item.js:

$(function(){ 
    $('#useful_item_submit').tooltip(); 
    // among other useful javascripts that useful_items might use 
}); 

et à votre avis:

<% javascript 'useful_item' %> 
<div id="useful_item_form" > 
    <% form_for [flaggable, flaggable.useful_items.new] do |f| -%> 
     <div> 
      <%= f.submit "I find this useful", :title => 'Click to let others know you found this useful' %> 
     </div> 
    <% end -%> 
</div> 
Questions connexes