rspec

How to spec a scoped new object in the controller

How should I spec this nested build: #projects_controller.rb def new @account.projects.build end So far I have something like this: #projects_controller_spec.rb describe ProectssController do describe "GET new" do let(:account) { mock_model(Account) } let(:project) { mock_model(Project).as_null_object } before do ...

Rails 3 Rspec Error - SQLite3::SQLException: no such column: comments.article_id

I am trying to test for a comment deletion using Rspec. I think my test is wrong because when I try to delete a comment in my browser, it works. Here is the Rspec test that I am running: before(:each) do @admin = Factory(:user, :admin => true) @user = Factory(:user, :email => "[email protected]") @article = Factory(:arti...

How to stub an users_controller | authlogic, rspec2, rails3, factory_girl

My users_controller.rb # GET /users/1/edit def edit @user = current_user #@user = User.find(params[:id]) end my sweet looking users_controller_spec.rb ( notice all my commented out attempts ) describe "Authenticated examples" do before(:each) do activate_authlogic UserSession.create Factory.build(:valid_user) end des...

When writing a custom formatter is it possible to get the duration of each test?

In Spec::Runner::Formatter::BaseTextFormatter the method "def dump_summary(duration, example_count, failure_count, pending_count)" provides the parameter "duration" which seems to be the time it takes for all tests to execute. I would really like to know how long each test takes individually, is it possible to get this information? The m...

Speeding up RSpec tests in a large Rails application

I have a Rails application with over 2,000 examples in my RSpec tests. Needless to say, it's a large application and there's a lot to be tested. Running these tests at this point is very inefficient and because it takes so long, we're almost at the point of being discouraged from writing them before pushing a new build. I added --profile...

Ruby error: "Symbol as array index"

Hi, I am writing a spec for the create method of a controller : describe "POST create" do it "should create an adtag with valid params" do campaign = Campaign.make campaign_attributes = Hash.new campaign_attributes[:adtag_attributes] = Hash.new campaign_attributes[:adtag_attributes][:code] = "<h1>Sample co...

What does this default RSpec statement mean?

User.should_receive(:update_attributes).with({'these' => 'params'}) What does that statement mean? these isn't instantiated anywhere as meaning anything. The whole statement is this : describe "with valid params" do it "updates the requested user" do User.should_receive(:find).with("37") { mock_user } User.should_re...

What testing frameworks are used for Rails?

I am new to rails, but not to ruby, and the book I am reading (Pragmatic Programmers Agile Dev. with Rails 4th Ed.) just introduced me to unit tests in Rails. The book showed me the default rails testing suite (a subclass of Test::Unit). In the Rails community, is this the main testing framework that is used, because I use RSpec when doi...

In rails 3 and rspec 2, how can I test the layout used by a call to render() ?

I'm trying to test that a view was rendered with a given layout. How can I test which layout was used for the call to render() ? ...

shoulda should "be valid" do fails with "'handle_matcher': undefined method 'matches?'"

I'm trying out 'shoulda' on top of rspec (rails 3) with the following spec: require 'spec_helper' describe Article do should "be true" do assert true end end and it fails with /Users/jeppe/.rvm/gems/ruby-1.8.7-p302/gems/rspec-expectations-2.0.0.beta.20/lib/rspec/expectations/handler.rb:11:in `handle_matcher': undefined method...

An RSpec2 error by the biggest newb in New Jersey :( | factory_girl, authlogic, rails3

I'm writing an rspec scenario thats failing with: (#<User:0x1056904f0>).update_attributes(#<RSpec::Mocks::ArgumentMatchers::AnyArgMatcher:0x105623648>) expected: 1 time received: 0 times users_controller_spec.rb: describe "Authenticated examples" do before(:each) do activate_authlogic @user = Factory.create(:vali...

How do you stub a `current_user` with update_attributes set to false?

This is a pure syntactical question. I'm very new to RSpec. I basically want to write something on the lines of this erroring line : controller.stub!(:current_user(:update_attributes => false)) Anyone know how to properly write that? RSpec's default looks like this : User.stub(:find) { mock_user(:update_attributes => false) } ...

How to bypass including ControllerExampleGroup in a spec within a /controller directory

I have a module which is used in several controllers and is stored in app/controllers/. I want to create a spec for it in spec/controllers. The spec will be testing the module's functions, not an actual controller class. But, all specs in spec/controllers get the ControllerExampleGroup behaviors. And once that example is mixed in a con...

"Joe Smith" is not the same as Joe Smith in rspec?

Hey, I'm using TDD with rails for the first time... interesting concepts. Definitely useful. That is, until I come to this. When I run my test I get: 1) User should build the full name correctly Failure/Error: @u1.fullname.to_s.should be("#{@attr[:firstname]} #{@attr[:lastname]}") expected Joe Smith, got "Joe Smith" # ./spec...

Shoulda helpers don't work

I have one spec: require 'spec_helper' # hmm... I need to include it here because if I include it inside describe block `method_missing` exception raises. include Shoulda::ActionController::Matchers describe CategoriesController do include Devise::TestHelpers render_views context "new should render template :new" do setup ...

rpsec rails mocking session hash

I am trying to mock out the session hash for a controller like so: it "finds using the session[:company_id]" do session.should_receive(:[]).with(:company_id).and_return 100 Company.should_receive(:find).with(100) get 'show' end When I call get 'show' it states: received :[] with unexpected arguments expected: (:company_id) ...

rails3 rspec issue

Hey Guys! I am trying out rails3. I am using railstutorial site to explore more about rails3; the tutorial is very good to begin with (I have minimal experience with rails2). I have an issue with rspec which is currently blocking my progress. I saw that the tutorial recommended using rspec2.0.0.beta.18 gem; I instead installed rspec2.0...

Why is this recursion NOT infinite?

My friends and I are working on some basic Ruby exercises to get a feel for the language, and we've run into an interesting behavior that we're yet unable to understand. Basically, we're creating a tree data type where there's just one class, node, which contains exactly one value and an array of zero or more nodes. We're using rspec's...

Something wrong in my sequence of factory creation..

I was hoping someone would spot why this wouldn't work. I am getting an error thats being called because the attributes I specify with Factory_Girl are not being applied to the stub before validation. The Error: undefined method `downcase' for #<Category:0x1056f2f60> RSpec2 it "should vote up" do @mock_vote = Factory.create(:vote...

How do I test for belongs_to and has_many in Rails?

I'm using rspec and I'm trying to test whether or not my model y has many x. I've tried all sorts of things, including looping through the methods array, and can't seem to find a good method online. So what should I use? ...