Rails - Rspec/Capybara reporting fail unless sleep added -
i have page logged in user can set favourite item submitting ajax request calls next controller method:
def create item = item.find(params[:id]) if params[:commit] == 'save' item.unhide!(current_user) if item.is_hidden_by?(current_user) item.save_as_favourite_for!(current_user) else item.remove_as_favourite!(current_user) if item.is_favourite_of?(current_user) item.hide_for!(current_user) end respond_to |format| format.html { redirect_back_or items_path } format.js {render "create", locals: {item: item, index: params[:index].to_i || nil, page:params[:page]} } end end
this works expected, have request spec defined test front end end functionality:
describe "saving items", js: true let!(:current_user) {factorygirl.create(:user, email: "user@example.com", active: true)} let!(:favourite_item) {factorygirl.create(:item)} before { sign_in current_user } describe "when saving item" "should save user against item" expect click_button "save" # sleep(0.02) end.to change(favourite_item.savers, :count).by(1) end end end
notice commented out sleep(0.02) statement. if statement commented out, test (almost) fails, if include call, , end console interrupt, test passes every time. (any more time delay , fails).
is there improve way write test without workaround , why might failing without delay?
you can set wait time using dsl.
using_wait_time 3 expect click_button "save" end.to change(favourite_item.savers, :count).by(1) end
http://rubydoc.info/github/jnicklas/capybara/capybara.using_wait_time
the default wait time 2 seconds js tests if utilize rspec matcher, not test.
it'll pass if remove js: true
unless need js driver test.
ruby-on-rails rspec capybara
No comments:
Post a Comment