rspec2

Rails / RSpec: How to test #initialize method?

笑着哭i 提交于 2019-12-18 11:52:55
问题 How can I specify #initialize behaviour with RSpec? For example here: generator.rb class Generator attr_accessor :seed def initialize(seed = nil) @seed = seed || pick_seed end def pick_seed Time.now.to_i end end generator_spec.rb require 'generator' describe Generator it "calls 'pick_seed' method unless seed specified" do end end I'd like to set expectation that pick_seed method called from #initialize method. 回答1: For me, expectations are about designing conversations among collaborators. So

How to test after_sign_in_path_for(resource)?

孤者浪人 提交于 2019-12-17 18:39:36
问题 I have devise authentication and registration set up on my Rails app. I'm using after_sign_in_path_for() to customise the redirect when the user signs in based on various scenarios. What I'm asking is how to test this method? It seems hard to isolate since it is called automatically by Devise when the user signes in. I want to do something like this: describe ApplicationController do describe "after_sign_in_path_for" do before :each do @user = Factory :user @listing = Factory :listing sign_in

Specs2 breaks my test data, due to the way it works with iterator

时间秒杀一切 提交于 2019-12-14 02:43:47
问题 AFTER GETTING COMMENTS, AND FIGURED OUT HOW IT WORKS, I STILL THINK: Would be nice though, if specs2 provides non consumable logic, along with consumable for iterators. Like if I don't use iterator.size method directly, but use specs' method like: haveSize I have a test, which has a code: val ids = for(software <- parser) yield software.productID //ids.size must_== 2; ids.foreach(x => println(x)) It produces the output: 1 2 If I uncomment spec2 check ( ids.size must_== 2 ), it will provide

Trouble on testing a JavaScript redirection

可紊 提交于 2019-12-13 02:00:35
问题 I am using Ruby on Rails 3.1.0 and the rspec-rails 2 gem. I would like to test a JavaScript redirection but I am some trouble on doing that. In my controller file I have: respond_to do |format| format.js { render :js => "window.location.replace('#{users_url}');" } end In my spec file I have: it "should redirect" do xhr :post, :create response.should redirect_to(users_url) end If I run the spec I get the following: Failure/Error: response.should redirect_to(users_url) Expected response to be a

Cucumber and custom RSpec matchers

☆樱花仙子☆ 提交于 2019-12-13 01:34:26
问题 I'm trying to write a custom RSpec matcher for cucumber. I require cucumber/rails/rspec in env.rb, but I still get "uninitialized constant Spec::Matchers" error. I'm using latest versions of Rspec, Cucumber and CucumberRails. What am I missing? P.S. Same matcher works fine with pure RSpec... 回答1: Are you using RSpec 2? The correct constant is now RSpec::Matchers for that. Defined in rspec/matchers . If you're not, then perhaps you haven't required spec/matchers which defines the Spec:

Creating dynamic cucumber_steps to check for links on a page

不打扰是莪最后的温柔 提交于 2019-12-13 00:51:56
问题 Basically I want the scenario outline to loop through all of the various combinations so that I don't have to write a bunch of scenarios. I want to have it visit the starting page and then check to see if the links are there however I've hit a barrier. Cucumber feature Scenario Outline: As a user I need links in order to navigate the site Given I am a '<user>' When I am at the <page> page Then I should see a link to '<link>' Scenarios: As a user that is not logged in | user | page | link | |

page.should have_content - Special Characters causing error

末鹿安然 提交于 2019-12-12 13:53:15
问题 I am working on a basic Rspec test using Capybara that opens a webpage and checks that a string is contained within the home page. The tests require checking eaching of 8 different language sub domains on the sites and I have run into some problems with the special characters in the code. The following code works: it "should have an American Home page", :js => true do visit '/' page.should have_content('Workforce Management from InVision') end it "should have an English Home page", :js =>

undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x1057fd428> error while trying to set up RSpec with Devise

时间秒杀一切 提交于 2019-12-12 10:47:26
问题 I've got a spec/controllers/add_to_carts_spec.rb : require 'spec_helper' describe CartItemsController do before (:each) do @user = Factory(:user) sign_in @user end describe "add stuff to the cart" do it "should add a product to the cart" do product = FactoryGirl.create(:product) visit products_path(product) save_and_open_page click_on('cart_item_submit') end end end and /spec/support/spec_helper.rb : # This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] |

Can someone describe to me what RSpec 2 is doing in this?

只愿长相守 提交于 2019-12-12 05:38:15
问题 I have been attempting to dive into RSpec 2 but its auto generated controller specs do not work for any version of RSpec 2 with any version of Ruby or any version of Rails. Maybe I'm missing something obvious? def mock_category(stubs={}) @mock_category ||= mock_model(Category, stubs).as_null_object end describe "GET show" do it "assigns the requested category as @category" do Category.stub(:find).with("37") { mock_category } get :show, :id => "37" assigns(:category).should be(mock_category)

Stub a controller helper method in a helper spec

萝らか妹 提交于 2019-12-12 02:46:48
问题 In my application_controller.rb : helper_method :current_brand def current_brand @brand ||= Brand.find_by_organization_id(current_user.organization_id) end In my helper something_helper.rb def brands return [] unless can? :read, Brand # current_brand is called end I am writing a spec for something_helper and wish to stub current_brand describe SomethingHelper do before :each do helper.stub!(:can?).and_return(true) # This stub works end it "does the extraordinary" do brand = Factory.create(