2014-06-17 58 views
0

通過水豚rspec的色器件跡象這是我sign_in規格不知道如果我的測試頁工廠女孩

require 'spec_helper' 

describe 'user can sign in' do 
    before(:each) do 
    @user = Factory(:user) 
    visit ('/users/sign_in') do 
     #login_as(@user, :scope => :user) 
     fill_in('Email', :with => @user.email) 
     fill_in('Password', :with => @user.password) 
     click_button('Sign in') 
    end 
    expect(page).to have_content 'Signed in successfully' 
    end 
end 

我使用工廠女孩進行測試的用戶數據。

這裏是我的factories.rb

FactoryGirl.define do 
    factory :user do 
    email '[email protected]' 
    password 'gibberishdotcom' 
    password_confirmation 'gibberishdotcom' 
    end 
end 

我使用的數據庫清理清理測試環境。

這裏是我的spec_helper,

require 'simplecov' 
SimpleCov.start 

ENV["RAILS_ENV"] ||= 'test' 
require File.expand_path("../../config/environment", __FILE__) 
#require 'rspec/autorun' 
require 'rspec/rails' 
require 'capybara/rspec' 
require 'capybara/rails' 
require 'database_cleaner' 


include Warden::Test::Helpers 
Warden.test_mode! 

RSpec.configure do |config| 

    config.include Capybara::DSL 
    config.include FactoryGirl::Syntax::Methods 

    #use database cleaner instead 
    config.use_transactional_fixtures = false 

    config.before(:suite) do 
    DatabaseCleaner.strategy = :transaction 
    DatabaseCleaner.clean_with :truncation 
    end 

    config.before :each do 
    DatabaseCleaner.start 
    end 

    config.after :each do 
    DatabaseCleaner.clean 
    end 
end 

我想測試我的色器件登錄頁面。

我使用的軌道4,這裏是我的寶石

group :test do 
    gem 'rspec-rails', '~> 3.0.1' #bdd testing framework 
    gem 'capybara', '~> 2.3.0' #acceptance test framework 
    gem 'selenium-webdriver', '~> 2.42.0' #browser automation 
    gem "factory_girl_rails", "~> 4.0" #make sample data for tests 
    gem 'database_cleaner', '~> 1.3.0' #clean out test db environment 
end 

當我耙-t我得到這個我的服務器日誌

Judys-MacBook-Air:berklee-admissions-portal judyngai$ rake -t 
** Invoke default (first_time) 
** Invoke spec (first_time) 
** Invoke spec:prepare (first_time) 
** Execute spec:prepare 
** Invoke test:prepare (first_time) 
** Execute test:prepare 
** Execute spec 
/Users/judyngai/.rvm/rubies/ruby-2.1.0/bin/ruby -I/Users/judyngai/.rvm/gems/[email protected]/gems/rspec-core-3.0.0/lib:/Users/judyngai/.rvm/gems/[email protected]/gems/rspec-support-3.0.0/lib -S /Users/judyngai/.rvm/gems/[email protected]/gems/rspec-core-3.0.0/exe/rspec ./spec/controllers/omniauth_callbacks_controller_spec.rb ./spec/features/home_spec.rb ./spec/features/registrations_spec.rb ./spec/features/sign_in_spec.rb 
No examples found. 


Finished in 0.15487 seconds (files took 2.41 seconds to load) 
0 examples, 0 failures 
Coverage report generated for RSpec to /Users/judyngai/Desktop/berkeleycollegeofmusic/berklee-admissions-portal/coverage. 118/158 LOC (74.68%) covered. 
** Execute default 

我sign_in_spec.rb在規格/功能

它正在加載它,但它沒有看到它。這是我第一次使用工廠女孩。我嘲笑不正確?

回答

2

在功能規格中,您通常會爲斷言編寫方案。你可能想你的功能規格模型的更多是這樣的:

require 'spec_helper' 

feature 'user signs in' do 
    let!(:user) { FactoryGirl.create(:user) } 

    scenario 'with valid credentials' do 
    visit('/users/sign_in') 
    fill_in('Email', :with => user.email) 
    fill_in('Password', :with => user.password) 
    click_button('Sign in') 
    expect(page).to have_content 'Signed in successfully' 
    end 

    scenario 'with invalid password' do 
    visit('/users/sign_in') 
    fill_in('Email', :with => user.email) 
    fill_in('Password', :with => 'bananas') 
    click_button('Sign in') 
    expect(page).to have_content 'Email or password are invalid' 
    end 
end 

你會看到你的斷言通常遵循的模式expect(page).toexpect(page).to_not

例子:

expect(page).to have_content user.email 
expect(page).to have_css('.post', count: 1) 
+0

是什麼'讓(:用戶){廠(:用戶)}在'回報用戶?我正在爲@user取消未定義的電子郵件,因爲@user現在未定義。 – Jngai1297

+0

看我的編輯。對於那個很抱歉。 –

+0

我得到'未定義的方法工廠#' – Jngai1297