# Rspec e javascript

**URL:** <https://www.ruby-forum.com/t/rspec-e-javascript/206101>\
**Category:** IT\
**Created:** [April 22, 2011, 9:30am UTC](https://www.ruby-forum.com/t/rspec-e-javascript/206101 "2011-04-22T09:30:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [April 22, 2011, 9:30am UTC](https://www.ruby-forum.com/t/rspec-e-javascript/206101/1 "2011-04-22T09:30:03Z")

</div>

Dovrei testare, tramite rspec, un’action di un controller che viene  
chiamata tramite javascript.  
L’action e’:

def index  
unless params[:customer\_full\_name].blank?  
full\_name = params[:customer\_full\_name]  
@customers = Customer.search\_full\_name(full\_name).paginate(:per\_page  
=\> 10,  
:page =\> params[:page])  
else  
@customers = Customer.all.paginate(:per\_page =\> 10, :page =\>  
params[:page])  
end  
render :layout =\> false if request.xhr?  
end

nella view c’e’ un campo di ricerca che, man mano che si inseriscono  
dei valori, attiva una funzione javascript per una ricerca.  
Non riesco a far funzionare il test:

require ‘spec\_helper’  
require ‘capybara/rspec’

describe CustomersController do  
include Devise::TestHelpers

describe “search with js”, :js =\> true do  
before(:each) do  
@customers = mock\_model(Customer, :full\_name =\> “User-1”)  
end

```
context "when full_name is 'user'" do
  it "should find customers" do
    login
    page.should have_content "Customers"
    page.should have_content "Dashboard"
    click_link('Customers')
    fill_in "customer_full_name", :with => "User"
    @full_name = "User"
    Customer.should_receive(:search_full_name).with(@full_name).and_return(@customers)
    @customers.should_receive(:paginate)
    do_get
    assigns(:customers).should_not be_nil
  end
end

```

def login  
User.make!  
visit ‘/d/users/sign\_in/’  
fill\_in “user\_email”, :with =\> “removed\_email\_address@domain.invalid”  
fill\_in “user\_password”, :with =\> “password”  
click\_button “Sign in”  
end

def do\_get page= nil, format = ‘html’  
get ‘index’, :customer\_full\_name =\> @full\_name, :format =\> format  
end

Potete darmi qualche suggerimento?
