硒测试显示已经采取的电子邮件

时间:2011-07-04 10:44:40

标签: ruby-on-rails-3 selenium automated-tests

我在Rails 3应用程序中运行测试。我使用rspec,capybara和Selenium RC。

我对表单进行了以下测试:

require 'spec_helper'
require 'support/database_cleaner.rb'
describe "Greetings" do
  attr_reader :selenium_driver
  alias :page :selenium_driver

  before(:all) do
    selenium_setup
    @selenium_driver.start_new_browser_session
  end

  after(:all) do
    @selenium_driver.close_current_browser_session
    @verification_errors.should == []
  end

  describe "greeting creation" do

 before(:each) do
    @board = Factory(:board)
  end

 describe "success" do
      it "should create and display the greeting" do
           page.open "/boards/#{@board.id}"
           page.click "greeting_link"
           wait_for_ajax
           page.type "greeting_headline", "Hey Have a great day!!!"
           page.type "greeting_content", "I wish you the best birthday ever. Your are a great friend and deserve a great day."
           page.type "user_name", "Example User"
           page.type "user_email", "it@it.com"
           page.click "commit"
           wait_for_ajax
           ("Add greeting").should == page.get_text("greeting_link")
           #("Hey Have a great day!!!").should == page.get_text("css=div.headline.round")
           #page.is_text_present("Hey Have a great day!!!\n\n I wish you the best birthday ever. Your are a great friend and deserve a great day.").should be_true
           #("Hey Have a great day!!!").should == page.get_text("css=div.headline.round")
      end
    end

如果我在浏览器中手动执行此操作,则可以完美地运行。 但是,当我运行测试时,它会失败,因为验证表明电子邮件已被删除。

我试过rake db:test:prepare。没有快乐。同样的问题。

我在测试环境中安装了database_cleaner gem,并在我的spec / support / database_cleaner.rb中有以下内容

DatabaseCleaner.strategy = :truncation

RSpec.configure do |config|
  config.use_transactional_fixtures = false
  config.before :each do
    DatabaseCleaner.start
  end
  config.after :each do
    DatabaseCleaner.clean
  end
end

我在这里完全失败,因为我是NOOB。

有人能指出我正确的方向来解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

然后有两个显而易见的可能性 - 一个是由于某种原因工厂被调用两次(在这种情况下放置类似的东西

sequence(:email) {|n| "it@it#{n}.com"}

在你的工厂里应该解决这个问题。其他可能性是您的工厂定义了一个电子邮件地址it@it.com - 在这种情况下,当您尝试保存在测试中创建的地址时,您将遇到问题

答案 1 :(得分:0)

这是我的配置块 - config.use_transactional_fixtures = false`

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

config.before(:each) do
  if example.metadata[:js]
    DatabaseCleaner.strategy = :truncation
  else
    DatabaseCleaner.start
  end
end

config.after(:each) do
  DatabaseCleaner.clean
  if example.metadata[:js]
    DatabaseCleaner.strategy = :transaction
  end
end

end`

我真的不明白为什么会有所作为,但值得将它放入你的并给它旋转......