rspec

Testing a build association using Rspec in Rails 3

北慕城南 提交于 2020-01-05 06:02:50
问题 I have the following line in my create action of my teachers controller. @rating = @teacher.ratings.build(params[:rating]) unless params[:rating][:rating].blank? I know my associations are correct because this line correctly creates a new rating alongside a new teacher unless the rating is left blank. However I'm trying my best to follow TDD and I have no clue as to how to test that line using rspec. I'm kind of at a loss. I'm using factory girl and shoulda if that helps. 回答1: You can say

Where is the have_fields RSpec matcher defined?

拈花ヽ惹草 提交于 2020-01-05 05:45:07
问题 A recent question on using rspec featured some code with a have_fields matcher in it. A search indicated that have_fields was referenced in the mongoid-rspec gem, but when I went to GitHub, I could only find it referenced in the spec files. Similar searches of RSpec core and related gems came up empty as well. 回答1: You might be looking for the mongoid-minitest gem. In have_field.rb it declares have_fields as an alias: module Mongoid module Matchers module Document # TODO: Add documentation.

rails rspec - (2nd test) Expected response to be a <:redirect>, but was <200>

谁说我不能喝 提交于 2020-01-05 05:30:06
问题 Expected response to be a <:redirect>, but was <200> My tests has: describe "Link POST #create" do context "with valid attributes" do it "creates a new link" do expect{ post :create, link: FactoryGirl.create(:link, :group => @group) }.to change(Link,:count).by(1) end it "redirects to the new link" do post :create, link: FactoryGirl.create(:link, :group => @group) # response.should redirect_to @link # Link.unscoped.last response.should redirect_to Link.unscoped.last # render_template :show end

Can Nightwatch be used to test Rails?

那年仲夏 提交于 2020-01-05 04:21:09
问题 A contractor for our startup installed the Selenium-based Nightwatch testing framework, since our stack is React-heavy. But he told me that it could even be used to test our Rails code. A new contractor said, to the contrary, that Nightwatch couldn't do unit tests of our Rails controllers and models (which makes sense to me). Who is right? Do you suppose the first programmer had in mind just that we would do end-to-end testing (certain inputs lead to certain outputs), and that we need not

Ruby on Rails 3: has_one association testing

梦想的初衷 提交于 2020-01-05 03:45:06
问题 I may have my associations messed up. I have the following models: User and UserProfiles. My models: class User < ActiveRecord::Base has_one :user_profile, :dependent => :destroy attr_accessible :email end class UserProfile < ActiveRecord::Base belongs_to :user end I have a column named "user_id" in my user_profiles table. My factory is setup like so: Factory.define :user do |user| user.email "test@test.com" end Factory.sequence :email do |n| "person-#{n}@example.com" end Factory.define :user

Ruby on Rails 3: has_one association testing

大兔子大兔子 提交于 2020-01-05 03:44:12
问题 I may have my associations messed up. I have the following models: User and UserProfiles. My models: class User < ActiveRecord::Base has_one :user_profile, :dependent => :destroy attr_accessible :email end class UserProfile < ActiveRecord::Base belongs_to :user end I have a column named "user_id" in my user_profiles table. My factory is setup like so: Factory.define :user do |user| user.email "test@test.com" end Factory.sequence :email do |n| "person-#{n}@example.com" end Factory.define :user

HABTM Association with FactoryGirl

扶醉桌前 提交于 2020-01-05 03:35:12
问题 Have looked through and tried most examples and still cannot create a working HABTM . No matter how the CaseTrack and CaseTrackValue factories are constructed, I'm not able to find the CaseTrackValue[] in CaseTrack. Shouldn't creating CaseTrack properly provide a CaseTrackValue param within CaseTrack. BTW: the only working association for HABTM seems to be putting case_track_values { |a| [a.association(:case_track_value)] } in CaseTrack. class CaseTrack has_and_belongs_to_many CaseTrackValue

rspec: How to test redis#subscribe code?

夙愿已清 提交于 2020-01-05 03:00:10
问题 Given something like: class MyClass def subscribe $redis.subscribe('channel') do |on| on.message do |channel, msg| Something.create(msg) end end end end How can I test that when MyClass executes subscribe , it will run Something.create for each message it receives on the channel? 回答1: This code you have, it's not very testable. First of all, absolutely get rid of this global $redis variable. Instead, accept an instance of redis in the constructor. class MyClass attr_reader :redis def

RSpec - can't write unknown attribute (enum)

对着背影说爱祢 提交于 2020-01-04 17:32:09
问题 The model User: class User < ActiveRecord::Base enum my_enum: [ :some_value1, #.... ] Also I have a migration which adds my_enum to User: def change add_column :users, :my_enum, :integer end And the fixture for FactoryGirl: FactoryGirl.define do factory :user do email { Faker::Internet.email } password { Faker::Internet.password(10) } password_confirmation { password } my_enum { nil } end end Everything works fine. But when I run a test, I get an error: Failure/Error: paid_user = FactoryGirl

RSpec Rails Login Filter

限于喜欢 提交于 2020-01-04 14:14:33
问题 I recently switched started using rspec-rails(2.6.1) with my Rails(3.0.8) app. I'm used to Test::Unit, and I can't seem to get a filter working for my test methods. I like to keep things as DRY as possible, so I'd like to set up a filter that I can call on any test method that will login as an Authlogic user before the test method is called. I tried accomplishing this by using an RSpec filter in spec_helper.rb: config.before(:each, :login_as_admin => true) do post "/user_sessions/create",