rspec2

FactoryGirl + RSpec + Rails 3 'undefined method <attribute>='

爱⌒轻易说出口 提交于 2019-12-02 18:09:14
I'm fairly new to rails and TDD (as will no doubt be obvious from my post) and am having a hard time wrapping my brain around Rspec and FactoryGirl. I'm using Rails 3, rspec and factory girl: gem 'rails', '3.0.3' # ... gem 'rspec-rails', '~>2.4.0' gem 'factory_girl_rails' I have a user model that I've been successfully running tests on during development, but then needed to add an attribute to, called "source". It's for determining where the user record originally came from (local vs LDAP). In my factories.rb file, I have several factories defined, that look something like the following: # An

How to test ApplicationController method defined also as a helper method?

纵然是瞬间 提交于 2019-12-02 17:00:00
In my ApplicationController I have a method defined as a helper method: helper_method :some_method_here How do I test ApplicationController in RSpec at all? How do I include/call this helper method when testing my views/helpers? I'm using Rails3 with RSpec2 Jimmy Cuadra You can use an anonymous controller to test your ApplicationController, as describe in the RSpec documentation. There's also a section on testing helpers . You can invoke your helper methods on subject or @controller in the specification. I have been looking for a solution to this problem and anonymous controller was not what I

RoR: testing an action that uses http token authentication

冷暖自知 提交于 2019-12-02 16:46:22
I'm trying to test a controller that's using an http token authentication in the before filter. My problem is that it works ok wheh I use curl to pass the token, but in my tests it always fails (I'm using rspec btw). Tried a simple test to see if the token was being passed at all, but it seems like it's not doing so. Am I missing anything to get the test to actually pass the token to the controller? Here's my before filter: def restrict_access authenticate_or_request_with_http_token do |token, options| api_key = ApiKey.find_by_access_token(token) @user = api_key.user unless api_key.nil? @token

How to load a spec_helper.rb automatically in RSpec 2

痞子三分冷 提交于 2019-12-02 16:32:00
When developing gems in Ruby, I almost always need a file in which I can configure RSpec to my needs and maybe before doing that, require some helper modules which should be available in all my spec examples. In Rails applications a file named spec/spec_helper.rb is used for that. One thing that annoys me is that in the typical Rails environment, you have to require this spec_helper.rb file in every file that contains examples for it to be loaded. In the past I had a lot of problems with this related to changing load paths and relative require paths inside the example files. Now for my gems, I

Rails 3.1, RSpec: testing model validations

时光怂恿深爱的人放手 提交于 2019-12-02 16:20:31
I have started my journey with TDD in Rails and have run into a small issue regarding tests for model validations that I can't seem to find a solution to. Let's say I have a User model, class User < ActiveRecord::Base validates :username, :presence => true end and a simple test it "should require a username" do User.new(:username => "").should_not be_valid end This correctly tests the presence validation, but what if I want to be more specific? For example, testing full_messages on the errors object.. it "should require a username" do user = User.create(:username => "") user.errors[:username]

How can I tell Rails to use RSpec instead of test-unit when creating a new Rails app?

早过忘川 提交于 2019-12-02 15:45:01
I have test-unit installed and rspec installed (along with -core , -expectations , -mocks and -rails version 2.6.x). When I run the command rails new foo , it uses test-unit to generate the test stub files instead of rspec . Is there an option where I can tell rails to use rspec to generate the tests instead? The following should work: at command line: rails new MYAPP -T # The -T option tells rails not to include Test::Unit in Gemfile: gem 'rspec-rails' at command line: bundle install rails g rspec:install Create your new rails application as: rails new <app_name> -T Or remove your test

RSpec: How to write unit test case to receive an exception which is getting raised in private method

限于喜欢 提交于 2019-12-02 11:25:54
问题 I have implemented Optimistic Locking for Race Condition. For that, I added an extra column lock_version in the Product. Method: recalculate is calling private method_1 then it saves ( save! ) the product. I can't use save! in private method_1 , because it will fail the other things. I don't want to restructure the business logic. #Product: Model's new field: # lock_version :integer(4) default(0), not null def recalculate method_1 self.save! end private def method_1 begin #### #### if self

RSpec: How to write unit test case to receive an exception which is getting raised in private method

那年仲夏 提交于 2019-12-02 05:37:16
I have implemented Optimistic Locking for Race Condition. For that, I added an extra column lock_version in the Product. Method: recalculate is calling private method_1 then it saves ( save! ) the product. I can't use save! in private method_1 , because it will fail the other things. I don't want to restructure the business logic. #Product: Model's new field: # lock_version :integer(4) default(0), not null def recalculate method_1 self.save! end private def method_1 begin #### #### if self.lock_version == Product.find(self.id).lock_version Product.where(:id => self.id).update_all(attributes)

Rails 4 and RSpec, undefined method `assertions' in routing spec

不问归期 提交于 2019-12-01 19:46:26
问题 I'm having what appears to be the same issue as undefined method `assertions' in routing spec but I'm running Rails 4 and it is locked to minitest 5.0 or greater. Thus the solution to use minitest 4.7 I can't make work. Is there a work around for this? Where would I report the bug? 回答1: This problem is caused by a change in minitest 5.0 documented here: # https://github.com/seattlerb/minitest/issues/286 is fixed by using: gem "rspec-rails", '~> 2.14.0.rc1' in the gemfile. 来源: https:/

How to use the test database with Capybara?

人盡茶涼 提交于 2019-12-01 17:26:11
I'm using RSpec, Spork, Capybara and Capybara Mechanic to write integration tests for a registration path that uses Facebook connect. It correctly navigates through registration, but the new users are created in my development database instead of the test database. RSpec is setup to use the test environment and I've confirmed that any model methods I run in my spec all hit the test database, but all of the UI actions hit development. Here's my test: it "go through registration path" do print "RAILS_ENV: #{Rails.env}\n" # correctly prints 'test' print "1) #{User.count}\n" # correctly shows the