rspec

What am I doing wrong with this rspec helper test?

拥有回忆 提交于 2019-12-23 19:49:38
问题 All I'm trying to do is spec how a one line helper method for a view should behave, but I'm not sure what kind of mock object, (if any) I should be creating if I'm working in Rails. Here's the code for events_helper.rb: module EventsHelper def filter_check_button_path params[:filter].blank? ? '/images/buttons/bt_search_for_events.gif' : '/images/buttons/bt_refine_this_search.gif' end end And here's my spec code, in events_helper_spec.rb: require File.expand_path(File.dirname(__FILE__) + '/..

rspec mocks: verify expectations in it “should” methods?

对着背影说爱祢 提交于 2019-12-23 19:31:23
问题 I'm trying to use rspec's mocking to setup expectations that I can verify in the it "should" methods... but I don't know how to do this... when i call the .should_receive methods on the mock, it verifies the expected call as soon as the before :all method exits. here's a small example: describe Foo, "when doing something" do before :all do Bar.should_recieve(:baz) foo = Foo.new foo.create_a_Bar_and_call_baz end it "should call the bar method" do # ??? what do i do here? end end How can i

How to raise an exception in an RSpec test

青春壹個敷衍的年華 提交于 2019-12-23 18:52:23
问题 I'm stuck on a test scenario. I have a controller method: def update @object = Object.find params[:id] # some other stuff @object.save rescue ActiveRecord::StaleObjectError # woo other stuff end The first part I test with: context '#update' let(:object) { double } it 'nothing fails' do # some other stuff expect(Object).to receive(:find).with('5').and_return(object) expect(object).to receive(:save).and_return(true) xhr :put, :update, id:5 expect(response).to be_success expect(assigns(:object))

Stubbing File.open with Rspec

青春壹個敷衍的年華 提交于 2019-12-23 17:53:05
问题 I'm attempting to stub File.open in order to test a method I have that reads a CSV file. Here's the model: class BatchTask def import(filename) CSV.read(filename, :row_sep => "\r", :col_sep => ",") end end Here's the spec code: let(:data) { "title\tsurname\tfirstname\rtitle2\tsurname2\tfirstname2\r"} let(:result) {[["title","surname","firstname"],["title2","surname2","firstname2"]] } it "should parse file contents and return a result" do File.stub(:open).with("file_name","rb") { StringIO.new

Capybara + Poltergeist with Database-cleaner not finding FactoryGirl records

懵懂的女人 提交于 2019-12-23 17:24:51
问题 I'm writing integration tests and creating records with FactoryGirl . My controllers are throwing RecordNotFound when the test has js: true (with Poltergeist) even though they are found in a non-js test (without Poltergeist). I do have use_transactional_fixtures set to false and DatabaseCleaner.strategy set to :truncation , which seemingly covers every existing SO question on this issue. I tried substituting Selenium (with Firefox) for Poltergeist but I get the same result. ETA I started a

Capybara and before(:all) in rspec

独自空忆成欢 提交于 2019-12-23 15:29:08
问题 Here's my complete spec: require 'spec_helper' describe "Idea page", js: true do subject { page } before(:all) do create(:idea) visit root_path click_link "Log In" end context "Single idea" do before do page.find(:xpath, '//*[@id="accordion"]/div/div[1]/a').click end it { should have_selector('a',text:'Claim') } it "should have a button for reporting the idea" it "should have a button for opening all links" describe "Claiming" do before do click_link "Claim" end it {should have_selector('a',

WebMock: Rspec - Test Facebook Validation by using JSON response

落爺英雄遲暮 提交于 2019-12-23 13:24:39
问题 I am having difficulty to write specs for my program as I need to validate the facebook ID after I filled in all the correct format information. My facebook ID is retrieved from JSON response so I not sure how do I portray this in my specs. I am very new to this field and I had been stuck at this point for a long time. I will be grateful if there are some sample codes to help me in this. Below are my half-done specs. Scenario: Use user given Fbid to validate facebook page by using "http:/

RSpec test PUT update action

好久不见. 提交于 2019-12-23 13:22:44
问题 I am trying to write some RSpec tests to test my app, but I've stumbled on several problems that I can't find any solution. 1) I am trying to test the update action. Here is my code: it "email is a new one" do put :update, id: @user, user: FactoryGirl.attributes_for(:user, :email=>"a@b.c") @user.reload @user.email.should == "a@b.c" puts @user.email end Here is the UsersController update action: def update @user = User.find(params[:id]) respond_to do |format| if @user.update_attributes(params[

In RSpec/Rails acceptance tests,is there any way to push messages to the RSpec output and maintain the nice indentation?

杀马特。学长 韩版系。学妹 提交于 2019-12-23 13:01:02
问题 My acceptance tests (RSpec/Capybara) have some long sequences of steps all under a single it...do block, and I'd like to manually add some additional documentation of each step into the RSpec documentation output. So where the RSpec output (in documentation format) currently looks like: FooController New user creates account and creates profile it passes Within the long sequence of steps I'd like to push some additional info to the output: FooController New user creates account and creates

How to count RSpec examples filtered with :focus in a git hook?

旧街凉风 提交于 2019-12-23 12:10:10
问题 I am trying to write a Git pre-commit hook that would not let the user commit if there is an example that is tagged with :focus . Using RSpec's API (okay even if it is private), is there any way to find out the number of examples with the :focus filter? I found the example_count-instance_method. It could be useful but I'm not sure how it can be called from an external script. 回答1: Here is an Overcommit pre_commit hook that uses RSpecs private API to find out specs with :focus filter: require