Git Product home page Git Product logo

Comments (8)

hymRedemption avatar hymRedemption commented on September 19, 2024

我能想到最简单的方法就是 before(:each) 的时候 chdir 到测试的目录,after(:each)的时候 chdir 回来,但是不知道有没有其他的副作用

from kano.

nobrick avatar nobrick commented on September 19, 2024

Taxon model 中的一些 validation,会用到 Taxon::Config 提供的方法,但是这里用 Rspec 提供的 Mock 机制我实验了一下并不起作用,因为 Rspec mock 都是在测试代码中显示调用才会起作用,而对于代码内部(例如 Taxon 的 methods 内部去调用 Taxon::Config)调用时,mock 机制并不起作用。

@hymRedemption 应该不是 mock 不起作用的原因吧,在一个 test case 中应用 allow / receive / and_return 以后,所有相关的调用应该都会生效,不论是否在测试代码中,比如你说的 Taxon 的类方法,内部调用了 Taxon::Config,只要在当前 test case 中调用之前有过 mock,就会生效。

from kano.

nobrick avatar nobrick commented on September 19, 2024

当 Taxon.update(certified_statuses: :a) 的时候,会抛出异常(因为 validates :certified_statuses :inclusion { in: Taxon::Config.certified_statuses }),其说明 certified_statuses 只能是在 config/taxon.yml 中我们定义的文件中的那些取值。

关于你后面举得的这个例子,应该还是提前调用的问题,validations 已经把 mock 之前的状态读入进去了。

from kano.

nobrick avatar nobrick commented on September 19, 2024

解决这个 validations 的问题我倒是没有遇到过,可能需要禁用原有的 validations,然后再重新创建你需要的。

不过我觉得关于测试 Taxon Config 方面,应该还是不需要专门创建相关的文件,因为文件读取这个系统调用本身是没有任何问题,并不需要测试。mock 也仅仅需要一行代码,并不复杂,甚至要更简单一些,而且不借助外界系统调用运行效率也会高很多。

from kano.

nobrick avatar nobrick commented on September 19, 2024

另外想到:关于你提到的

代码内部(例如 Taxon 的 methods 内部去调用 Taxon::Config)调用时,mock 机制并不起作用。

会不会是因为没有考虑 reset 相关的类变量?因为在 taxons_config 以外的方法都使用了 ||= 操作符,所以可以在原来的类中建立一个 .reset_taxons_config 的方法,把所有的 @@variable 设置为 nil

from kano.

nobrick avatar nobrick commented on September 19, 2024

@hymRedemption

from kano.

hymRedemption avatar hymRedemption commented on September 19, 2024

@nobrick 我试试看看

from kano.

nobrick avatar nobrick commented on September 19, 2024

OK~

On Feb 1, 2016, at 7:27 PM, HS [email protected] wrote:

@nobrick
我试试看看


Reply to this email directly or view it on GitHub
.

from kano.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.