【问题标题】:Rspec - save function not recognizedRspec - 无法识别保存功能
【发布时间】:2014-02-06 07:13:03
【问题描述】:

以前有效的“保存”等基本数据库方法不再有效,我找不到现有的问题来解决原因。

运行 bundle exec rails spec/ 会在许多位置引发未定义的 nil:Nilclass 错误保存。

c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:242:in 'module_eval' c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:242:in 'subclass' c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:228:in 'describe 等等……

会不会是 gem 版本不匹配造成的?

我使用 postgresql 的测试和开发数据库是最新的并已完全迁移。

我确定有

config.include Capybara::DSL

在我的 spec_helper.rb 文件中。

user_spec.rb
.
.
.
describe "with mixed case" do
        let(:mixed_case_email) { "FooBaR@GmAIL.CoM" }
        before { @user.email = mixed_case_email }

        describe "should be saved as all lower-case" do
          @user.save
          expect(@user.email).to eq mixed_case_email.downcase               
        end     

        describe "and that's already taken" do
                before { user_with_same_email = @user.dup 
                             user_with_same_email.email
                             user_with_same_email.save }
                describe "should be invalid" do                     
                    it { should_not be_valid }
                end
        end             
.
.
.   

【问题讨论】:

  • 你不能只在你的 Rspec 代码中间放随机代码。您需要将 user_with_same_email.email; userr_with_same_email.save 放在测试的上下文中。
  • 我把代码改成了上面的。 nil:NilClass 的保存未定义方法仍然存在。

标签: ruby-on-rails rspec capybara


【解决方案1】:

“保存”方法在 Rspec 中运行良好。它还有其他问题。

描述“任何事情都在这里......”做 在做之前 @p = Project.create(:name => 'logtasks') @p.save 结尾 它 { 期望(@p.name).to eq 'logtasks' } 结尾

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 2021-01-22
    • 2019-08-01
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多